Home | 5.1 | 5.2 | 5.4 | 5.41 | 5.5 | 5.6 | 5.7 | 5.8 | 5.9 | HW |
Static Variables and Methods
- What are static variables?
- Cool, but why did I have to use a static variable?
- Popcorn hacks:
- What are static methods?
- Here’s an example:
- Why did I use a static method?
- Popcorn hack:
What are static variables?
Static variables: belong to the class rather than a particular instance.
- These types of variables are variables shared across all instances of a class.
import java.util.ArrayList;
import java.util.List;
public class Gadget {
public static int totalGadgets = 0; // Static variable to track total gadgets made
private String gadgetName; // Instance variable to store the name of the gadget
//public static List<Gadget> gadgetsList = new ArrayList<>(); // Static list to track all gadgets
// Constructor to set the gadget name and increment totalGadgets
public Gadget(String gadgetName) {
this.gadgetName = gadgetName;
totalGadgets++; // Increment the total gadgets count
// gadgetsList.add(this); // Add this gadget to the static list
}
}
// In the Main class:
public class Main {
public static void main(String[] args) {
// Create three gadgets
Gadget g1 = new Gadget("Freeze Ray");
Gadget g2 = new Gadget("Banana Blaster");
Gadget g3 = new Gadget("Lipstick Taser");
// Print the total number of gadgets
System.out.println("Total gadgets made: " + Gadget.totalGadgets);
}
}
Main.main(null);
Total gadgets made: 3
Cool, but why did I have to use a static variable?
- The totalGadgets was made as a static variable because it is tracking data that is shared across all instances of the Gadget class.
- If totalGadgets was an instance variable, then it would always be 1 since it would only reflect how many gadgets are in the specific instance.
Popcorn hacks:
- Look at some of the code I’ve commented out and try experimenting with gadgetsList if you want. Otherwise, just make a static variable that serves a purpose in the program.
public class Minion {
// Static variable to count the number of minions
public static int minionCount = 0;
// Instance variables
private String name;
// Constructor
public Minion(String name) {
this.name = name;
minionCount++; // Increase count whenever a new minion is created
}
// Method to get the total number of minions
public static int getMinionCount() {
return minionCount;
}
public static void main(String[] args) {
// Create some minions
Minion kevin = new Minion("Kevin");
Minion stuart = new Minion("Stuart");
// Print the total number of minions
System.out.println("Total minions: " + Minion.getMinionCount());
}
}
Minion.main(null);
Total minions: 2
What are static methods?
Static methods are associated with the class and not any object of the class
- Static methods can only directly access other static methods and static variables of the class.
- They cannot use the “this” keyword because they don’t belong to any instance.
Here’s an example:
import java.util.ArrayList;
import java.util.List;
public class Gadget {
public static int totalGadgets = 0; // Static variable to track total gadgets made
private String gadgetName; // Instance variable to store the name of the gadget
public static List<Gadget> gadgetsList = new ArrayList<>(); // Static list to track all gadgets
// Constructor to set the gadget name and increment totalGadgets
public Gadget(String gadgetName) {
this.gadgetName = gadgetName;
totalGadgets++; // Increment the total gadgets count
gadgetsList.add(this); // Add this gadget to the static list
}
// Getter for the gadget name
public String getGadgetName() {
return gadgetName;
}
// Static method to print all gadgets in the list
public static void printAllGadgets() {
System.out.println("Gadgets created:");
for (int i = 0; i < gadgetsList.size(); i++) {
System.out.println("- " + gadgetsList.get(i).getGadgetName());
}
}
}
// In the Main class:
public class Main {
public static void main(String[] args) {
// Create three gadgets
Gadget g1 = new Gadget("Freeze Ray");
Gadget g2 = new Gadget("Banana Blaster");
Gadget g3 = new Gadget("Lipstick Taser");
// Print the total number of gadgets
System.out.println("Total gadgets made: " + Gadget.totalGadgets);
// Print all gadgets stored in the static list
Gadget.printAllGadgets();
}
}
Main.main(null);
Total gadgets made: 3
Gadgets created:
- Freeze Ray
- Banana Blaster
- Lipstick Taser
Why did I use a static method?
- Static methods can only directly access other static variables and methods, such as gadgetsList
- I needed to print inforamtion about all instances of the Gadget class, which applied to the entire class as a whole.
Popcorn hack:
Dr. Nefario and Gru need to calculate the cost of their equipment to remain under the budget for this year! Add a second parameter to the Gadget constructor to include cost for Gadget instances, and make a static method to calculate the price of all gadgets that have been made so far.
public class Gadget {
// Instance variables
private String name;
private double cost;
// Static variable to keep track of total cost
public static double totalCost = 0;
// Constructor to initialize name and cost of each gadget
public Gadget(String name, double cost) {
this.name = name;
this.cost = cost;
totalCost += cost; // Add the cost of this gadget to the total cost
}
// Static method to get the total cost of all gadgets
public static double getTotalCost() {
return totalCost;
}
public static void main(String[] args) {
// Create some gadgets with name and cost
Gadget laserGun = new Gadget("Laser Gun", 500.50);
Gadget jetpack = new Gadget("Jetpack", 1200.75);
Gadget bananaLauncher = new Gadget("Banana Launcher", 299.99);
// Print the total cost of all gadgets
System.out.println("Total cost of all gadgets: $" + Gadget.getTotalCost());
}
}
Gadget.main(null);
Total cost of all gadgets: $2001.24