Welcome to Lesson 10!
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner;
|
Static Variables
- A class can have static variables as well as static methods
- Like static methods, a static variable belongs to the class as a whole and not to objects individually
- Another way to think of it: the static variable is shared among all objects of the class, rather than belonging to just one, individual object.
- With instance variables, each object has its own copies
- However, with static variables all objects share the variable
Writing Static Variables
- You write static variables like instance variables, but with the keyword
static
added - For example:
private static int numObjs;
- If you do not initialize static variables, then Java initializes them just like instance variables
- Numerical values are initialized to
0
, boolean values tofalse
and objects tonull
- Like instance variables, all static variables should be declared private except for constants
- Oftentimes you declare constant variables
static
(andpublic
) - For example:
public static final double interestRate = 0.015;
Accessing Static Variables
- Any method can access the value of a static variable
- This includes static methods as well as instance methods
- A classic example of using static fields and methods is to keep track of how many objects of a class are constructed
- We first add an object count variable to the class:
private static int numObjs = 0;
- We need to increment the variable whenever a new object is instantiated
- Thus we increment the static variable in the every constructors
public ConstructionCounter() { numObjs++; }
- Also need a method to retrieve the count
public static int getNumObjs() { return numObjs; }
- Since the
getNumObjs()
method is static, we can call it without instantiating an object:System.out.println("Initial count: " + getCount());
- After instantiating several objects, we call the method again to retrieve the count of objects instantiated
- We can experiment with the following example code
Example Using static
Methods and Variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class ConstructionCounter {
private static int numObjs = 0;
public ConstructionCounter() {
numObjs++;
}
public static int getCount() {
return numObjs;
}
// For testing
public static void main(String[] args) {
System.out.println("Initial count: "
+ getCount());
ConstructionCounter c1 =
new ConstructionCounter();
ConstructionCounter c2 =
new ConstructionCounter();
System.out.println("Number constructed: "
+ getCount());
}
}
|
Static Dos and Don'ts:
- Note that static methods canNOT call regular methods without creating instance of the class.
- This is why you cannot call regular methods from the
main()
method - If you try to call a regular method from a static method (without creating an instance of the class), you get a message like:
non-static method cannot be referenced from a static context
- For example:
public static void main(String[] args) {
CoffeeBar bar = new CoffeeBar();
Coffee cup1 = new Coffee();
System.out.println("Before making coffee: " + cup1.getFlavor() + "\n");
//cannot call a non static method from a static method
makeCoffee(cup1, "Mocha Latte"); //will cause error
}
public void makeCoffee(Coffee cup, String flavor) {
cup.setFlavor(flavor);
addTopping(cup, "cinnamon"); //okay to call static method from non-static method
printOrder(cup); //okay to call a non-static method from a non-static method
}
public static void addTopping(Coffee cup, String topping) {
cup.setFlavor(cup.getFlavor() + " with " + topping);
}
public void printOrder(Coffee cup) {
System.out.println("Your ordered: " + cup.getFlavor());
}
}
class Coffee {
private String flavor;
public Coffee() {
setFlavor("flavor unknown"); //okay to call a non-static method from a non-static method
}
public Coffee(String flavor) {
setFlavor(flavor); //okay to call a non-static method from a non-static method
}
public String getFlavor() {
return flavor;
}
public void setFlavor(String newFlavor) {
flavor = newFlavor;
}
}
private int numObjs;
public static int getNumObjs() { //will not compile
return numObjs;
}
Activity 10.1: A Person Class with Static Variable and Method (10 pts)
- Open up your Person.java class from the previous activity.
- Add a static variable to the top of your Person class:
private static int numPersons = 0;
- Next, add two static methods to access and update the value of the numPersons variable:
- Write a static access method as follows:
- This accessor method is named getNumPersons
- It is a static method
- It returns an int
- The body of the method returns the value of the numPersons variable
- Write a static mutator method as follows:
- This mutator method is named updateNumPersons
- It is a static method
- It returns nothing
- The body of the method increments the value of numPersons by 1
- Copy and paste the new test file below into PersonTest.java (you can erase the old contents of the file):
/**
* PersonTest.java, Activity 10.1
* @author
* CIS 36B
*/
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name, gender, street;
int age, number;
Person person1;
System.out.print("Welcome!\n\nEnter your name: ");
name = input.nextLine();
System.out.print("Enter your age: ");
age = input.nextInt();
System.out.print("Enter your gender: ");
gender = input.next();
System.out.print("Enter the street number of your address: ");
number = input.nextInt();
System.out.print("Enter the name of your street: ");
input.nextLine();
street = input.nextLine();
Address address = new Address(number, street);
person1 = new Person(name, age, gender, address);
Person.updateNumPersons();
System.out.println("\nYour Summary:\n" + person1);
Person person2 = new Person(person1);
person2.setName("C.J. Nguyen");
Address a = new Address(42, "South 3rd St");
person2.setAddress(a);
Person.updateNumPersons();
System.out.println("\nAnother student with your "
+ "same age and gender: \n" + person2);
System.out.println("\nTotal num persons: " + Person.getNumPersons());
input.close();
}
}
* PersonTest.java, Activity 10.1
* @author
* CIS 36B
*/
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name, gender, street;
int age, number;
Person person1;
System.out.print("Welcome!\n\nEnter your name: ");
name = input.nextLine();
System.out.print("Enter your age: ");
age = input.nextInt();
System.out.print("Enter your gender: ");
gender = input.next();
System.out.print("Enter the street number of your address: ");
number = input.nextInt();
System.out.print("Enter the name of your street: ");
input.nextLine();
street = input.nextLine();
Address address = new Address(number, street);
person1 = new Person(name, age, gender, address);
Person.updateNumPersons();
System.out.println("\nYour Summary:\n" + person1);
Person person2 = new Person(person1);
person2.setName("C.J. Nguyen");
Address a = new Address(42, "South 3rd St");
person2.setAddress(a);
Person.updateNumPersons();
System.out.println("\nAnother student with your "
+ "same age and gender: \n" + person2);
System.out.println("\nTotal num persons: " + Person.getNumPersons());
input.close();
}
}
- When you get the correct output (as shown below), upload Address.java and Person.java to Canvas
Enter your name: Xing Li
Enter your age: 39
Enter your gender: M
Enter the street number of your address: 555
Enter the name of your street: W. Harlem Ave
Your Summary:
Name: Xing Li
Age: 39
Gender: M
Address: 555 W. Harlem Ave
Another student with your same age and gender:
Name: C.J. Nguyen
Age: 39
Gender: M
Address: 42 South 3rd St
Total num persons: 2
2. Automatic Garbage Collection
- What happens to an object that is no longer used?
- Java performs what is known as "automatic garbage collection" to remove those currently unused objects.
- In C and C++, special methods (free() and delete()) must be called by the programmer to deallocate memory -- return the memory for reuse by the program
- In
Java, the garbage collection process happens automatically, and thus
provides superior memory management (not prone to human error).
- In Java, an object is marked for garbage collection if there are no references to the object in the program.
Student s1 = new Student("Wen", 3.9);
s1 = null; //s1 no longer references object
- Additionally, as objects age, they are considered better and better candidates for garbage collection.
- Different parts of memory are reserved for objects of different ages.
- The Young Generation are newly created objects.
- The Old Generation are all objects older than a predefined threshold.
- The Permanent Generation are objects required by the JVM that are never garbage collected
- At certain intervals, the Garbage Collector moves through and marks objects that are currently unreferenced.
- To read more about the Automatic Garbage Collection process, see the Oracle documentation below
- You can manually choose to run the garbage collector
as well, although it is considered bad practice, and does not
necessarily speed up the garbage collection of an object.
System.gc();
image source
Oracle Tutorial on Garbage Collection:
Wrap Up:
- Answer the practice exam questions for this lesson on Canvas
Upcoming Assignments:
- Activity 10.1 due Thursday at 11:59pm
- Lesson 10 Practice Exam Questions due Thursday at 11:59pm
- Peer Reviews of Lesson 9 and 10 Practice Exam Questions due Saturday at 11:59pm
- Lab 5 due Monday at 11:59pm
~ Have a Great Weekend! ~