Classes
and Objects in Java
Java is an object oriented
programming language. It relates its concept to the real world. A java program
may consist of a single class or couple of classes but there is only one main
method in the program. Main() method may be in any class.
A class is the blueprint for
the object. It is also said as collection of objects. A class tells the compiler
how to create an object of particular type. In java everything is written inside
the class.
An object is an instance of
a class. Every object has its state and behavior.
An object has a set of
instance variable that are called its state and the method it calls are called
its behavior. It can also be said that what an object knows about itself is its
state and what an object does is its behavior.
How to create an object?
The object of a class is
created using new operator.
If there is class with name
Apple, then the object for this is created by writing :-
Apple one=new Apple();
What the above statement
does is, it creates a reference variable one that holds the reference for the
new object created for the class Apple.
The diagram below shows the
one reference to Apple object :-