A class in Java is much like one in C++. It consists of a group of related methods and variables lumped together under one name. The static class variables are for class-as-a-whole data. They are allocated only once at load time and are shared by all instances of objects of that class. The instance variables, are allocated inside each object of that class. Static class methods work when there is no current object. They can only reference static class variables and static methods, unless of course they allocate an object and then use explicit references to the instance variables. Instance methods work by default on the fields of the current this object.
Let’s say you had a TV class to deal with television sales. There are two kinds of variables:
static variables that track facts about all televisions in general, e.g. total sold or a list of manufacturers.
instance (non-static) variables that track facts about individual televisions, e.g. manufacturer, serial number, diagonal, type LCD/CRT etc.
The facts about televisions in general are stored in static variables, only one copy of the variable for the whole TV class. The facts about individual televisions are stored in instance variables TV objects instantiated with new. There is one copy of each instance field per TV object.
Similarly, there are static methods about televisions in general and instance methods about particular televisions. Unless you have a TV object, you can’t call any of the instance methods. You don’t need a TV object to call one of the TV static methods. static methods may only look at the static fields. Instance methods may look at either the static or instance fields.
There can be only one public class in each source file. If your class was called HelloWorld the name of the source file must be precisely HelloWorld.java with every letter exactly matching even in case. In Java, there is no such thing as a method or variable that does not belong to some class. Java comes with a built-in set of classes arranged in a class hierarchy.
Class vs Object
One of the fundamental things a newbie has to understand is the difference between a class and an object. It is very simple once you get your head around it. The class is like a blue print for a house, but the object is like a house itself. Using the same house blue print, you can create many instances of the house, each slightly different.
In addition, a house class maintains static information common to all house object instances. A house object contains its own instance information which may differ from other house objects. The code for the methods is attached to the class because the the code is the same for all house objects. The house class survives until the program ends.
However, the house objects are garbage collected after there no longer exist any references (pointers) to them. Classes come into being when you first use one of their methods, either a static method or a constructor. They are loaded into memory and stay there. Objects come into being when you instantiate them by using new to invoke a constructor.