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
Let’s say you had a TV class to deal with
television sales. There are two kinds of variables:
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.
- 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.
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 is also a class that will tell you various facts
about a given class.
Where Did that Class Come From?
Sometimes you want to know where a
class or resource came from. Which jar, which directory. This may help track down
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 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.
Oracle’s Javadoc on Class hierarchy
Oracle’s Javadoc on Class
class : available:
Oracle’s Javadoc on Class.forName
Oracle’s Javadoc on Class.getComponentType
Oracle’s Javadoc on Class.getDeclaringClass