"Let's move on to a new topic. Now, I'd like to discuss static variables and methods."

"Ellie, I've already learned about static variables and methods. But I'd like to learn more details."

"When we declare variables in a class, we define whether these variables will be created just once, or if each instance (object) of the class will have its own copy. By default, a new copy of a variable is created for each object. This is how it looks:"

Class declaration
class Cat                        // Class
{
    String name;                 // Variable

    Cat(String name)             // Constructor
    {
        this.name = name;        // Variable initialization
    }
}
Code in the main method:
Cat cat1 = new Cat("Oscar"); // Create one object whose name variable contains "Oscar"
Cat cat2 = new Cat("Missy"); // Create one object whose name variable contains "Missy"
System.out.println(cat1.name);
System.out.println(cat2.name);
Screen output
Oscar
Missy

"Despite being declared in the same class (Cat), the variables cat1.name and cat2.name contain different values because they reference different objects."
"That makes sense."
"However, only one copy of a static variable exists for every instance of a class, and it must be accessed using the class name."

Class declaration
class Cat                   // Сlass
{
    String name;            // Instance (non-static) variable
    static int catCount;    // Static variable

    Cat(String name)
    {
        this.name = name;
        Cat.catCount++;   // Increment the static variable by 1
    }
}
Code in the main method:
System.out.println(Cat.catCount);
Cat cat1 = new Cat("Oscar");

System.out.println(Cat.catCount);
Cat cat2 = new Cat("Missy");

System.out.println(cat1.name);
System.out.println(cat2.name);
System.out.println(Cat.catCount);
Screen output:
0
1
Oscar
Missy
2

"OK, that also makes sense."

"Java methods are divided into two categories. Instance methods are called on an object and have access to that object's data. Static methods don't have that access, since they simply don't have an object reference. However, they can reference the class's static variables and other static methods.

Static methods can't address non-static methods or non-static variables!"

"Why is that, Ellie?"

"Each instance variable is contained in an object. It can be accessed only if you have a reference to that object. No such reference is available to a static method."

"Do instance methods have such a reference?"

"Yes, it is passed to instance methods indirectly. A reference to the object on which an instance method is called is indirectly passed to the instance method. The variable that stores this reference is called this. This allows the method to always access the object's data or call another non-static method on the same object.

Instead of an object reference, null is passed to static methods. That's why they can't address non-static variables and methods. They simply don't have a reference to an object associated with these variables and methods."

"OK, Ellie, I understand that."

"This is how non-static methods work:

What the code looks like
Cat cat = new Cat();
String name = cat.getName();
cat.setAge(17);
cat.setChildren(cat1, cat2, cat3);
What really happens
Cat cat = new Cat();
String name = Cat.getName(cat);
Cat.setAge(cat, 17);
Cat.setChildren(cat, cat1, cat2, cat3);
When you call a method using <object> dot <method name>, you're actually calling a class method and passing that same object as the first argument. Inside the method, the object is called 'this'. All operations in the method are performed on this object and its data."

"Here's how static methods work:

What the code looks like
Cat cat1 = new Cat();
Cat cat2 = new Cat();
int catCount = Cat.getAllCatsCount();
What really happens
Cat cat1 = new Cat();
Cat cat2 = new Cat();
int catCount = Cat.getAllCatsCount(null);
When you call a static method, no object is passed to it. In other words, 'this' equals null. That's why a static method can't access non-static variables and methods (since it has nothing to implicitly pass to non-static methods)."

"A variable or method is static if it has the keyword static in front of it."

"Why are such methods needed if they are so severely limited?"

"Well, such methods do have their benefits."

"First, we don't have to pass an object reference to use static methods and variables."

"Second, it is sometimes necessary to have one and only one copy of a variable. For example, System.out (the static out variable of the System class)."

"Third, sometimes you need to call a method before you are able to create objects."

"Ellie, can you give me an example of this?"

"Why do you think the main() method is static? It's static so the programmer can call it immediately after loading a class into memory, before any objects have been created."