define behaviors of a class through static methods
define the static variables that belong to the class.
In Unit 2, we explored the Math class and its many static methods like Math.random(), and we've always used a main method which is static.
Static variables and methods belong to a class and are called with the Class Name rather than using object variables, like ClassName.methodName();
There is only one copy of a static variable or method for the whole class.
For example, the main method is static because there should only be one main method.
Static methods can be public or private.
The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations:
Static methods only have access to other static variables and static methods.
Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call other non-static methods.
However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.
Since there is only one copy of a static variable or method, static variables are often used to count how many objects are generated.
In the following class Person, there is a static variable called personCounter that is incremented each time the Person constructor is called to initialize a new Person object.
The static method printPersonCounter() prints out the value of the static variable personCounter.
Another common use for static variables is keeping track of a minimum or maximum value or an average of the values in a collection of objects.
SUMMARY
Static methods and variables include the keyword static before their name in the header or declaration. They can be public or private.
Static variables belong to the class, with all objects of a class sharing a single static variable.
Static methods are associated with the class, not objects of the class.
Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class.
Static methods cannot access or change the values of instance variables, but they can access or change the values of static variables.
Static methods cannot call non-static methods.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) In the last lesson, you wrote a class with methods to print out the song The Ants Go Marching.
Notice that this is a class where there are no instance variables and we don't really need to generate multiple objects.
With students or pets, it makes sense to have multiple objects.
With the Song, we can just make the methods static and have just 1 copy of them.
So, with that in mind:
Make a copy of your class from the previous lesson ("fork" your program in repl.it and it will make a copy of your program) and change the method(s) that print out the verses of the Song to be static.
Ask me if you're not sure how to "fork" your previous program.
In the main method, change how you will call the static methods by using just the class name instead of creating an object.
Add a public static variable called numVerses to the class that keeps track of the number of verses. Make sure to increment this variable in the verse method and print it out at the beginning of the verse.