Classes and Objects in Java

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 a couple of classes but there is only one main method in the program. The main () method may be in any class.
 
A class is a blueprint for the object. It is also said as a collection of objects. A class tells the compiler how to create an object of a 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 variables 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 a class with the name Apple, then the object for this is created by writing:-
  1. 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:-
 
image1.jpg