Ticker

6/recent/ticker-posts

Java (Object)

JAVA OBJECT





HOW TO DECLARE OBJECT IN JAVA
Object is an instance of a class and class like a blueprint for an object. 
An object represents its class and consists with certain number of class properties and behaviors.

Properties refer to the fields declared in the class and behavior represents to the methods declared and defined in the class.

In the real world, we can understand object as a cell phone that has its properties like: name, cost, color, shape, etc. and behavior like calling, chatting, ringing, etc.

So we can say that an object is a real world entity. 
Some real world objects are: ball, fan, car etc.


SYNTAX OF OBJECT IN JAVA
ClassName variable_name = new className();

In the above syntax, ClassName is name of class that can be anything (Like: Student, Teacher, School, So on) that we will declare in our example. 

variable_name is name of variable reference, that is used to hold the reference of created object.

The new is a keyword that is used to allocate memory for the object.

Lets see an example to create an object of class 'Student' that we created in the above class section.

Student ob = new Student();

Explanation: In the above code 'ob' an object that represents the 'Student' class during run-time.


USE OF NEW KEYWORD IN JAVA
The new keyword is used to create an actual physical copy of object and assign it, to the 'ob' object/variable. 
It will have physical existence and get memory in the heap area and the new operator dynamically allocates memory for an object.





















Post a Comment

0 Comments