Classes
Classes
A class creates a new data type that can be used to create objects.
When you declare an object of a class, you are creating an instance of that class.
Thus, a class is a logical construct. An object has physical reality. (That is, an
object occupies space in memory.)
The dot operator links the name of the object with the name of an instance
variable.
Although commonly referred to as the dot operator, the formal specification for
Java categorizes the . as a separator.
The 'new' keyword dynamically allocates(that is, allocates at run time)memory for
an object & returns a reference to it.
This reference is, more or less, the address in memory of the object allocated by
new.
This reference is then stored in the variable.
Thus, in Java, all class objects must be dynamically allocated.
You might be wondering why you do not need to use new for such things as integers
or characters.
The answer is that Java’s primitive types are not implemented as objects.
Rather, they are implemented as “normal” variables.
This is done in the interest of efficiency.
It is important to understand that new allocates memory for an object during run
time.
NOTE:
Bus bus = new Bus();
lhs(reference i.e. bus) is looked by compiler & rhs (object i.e. new Bus()) is
looked by jvm