Variables available only for the class are instance variables
Static variables can be modified by other objects, and accessed throughout the entire class since it is static, is shared by other instances of the same class (e.g s1, s2, etc)
Static methods can call other static methods, you don't have to go through the object to call on the method you can go through the class to call the method
public class Student{
//instance variables
private String name;
private int age;
private static int totalAge = 0; //class variables
public void addAgeTotal(int n){ //does not need to return total age because it is a static insstance variable
totalAge+=n;
}
public int getTotalAge(){
return totalAge;
}
public static int getTotalAge(){ // static method
}
}
//main class
public class studentMain{
public static void main(String[]args){
Student s1 = new Student("Alex", 17); //name, age as parameters
Student s2 = new Student("Bret", 18);
//these lines both print out 0 because they can be accessed by both objects from the same class
System.out.println(s1.getTotalAge());
System.out.println(s2.getTotalAge());
s1.addAgeTotal(s1.getAge()); //now age was added to age total, so the method above will print 17
s2.addAgeTotal(s1.getAge());
//now both lines will display 35
System.out.println(s1.getTotalAge());
System.out.println(s2.getTotalAge());
//now using the class method, we can change s1.getTotalAge()
System.out.println(Student.getTotalAge());````````
}
}