Objects and Classes in Java: Creation & Initialization

We are going to take the next level to our Java learning journey, so, today we are going to discuss basics of the Classes, thus, the agenda of today’s session is about Objects and Classes in Java. Before we begin our learning journey, I would like to recommend you an article about break statement, continue statement, comments, and Java doctool.

Objects and Classes are the foundation of Object-oriented programming language. The first step of your program starts with Classes, Constructors and Objects. Let’s begin our learning journey!

An understanding of Objects & Classes with real-time example

As we all know that Java is Object-oriented programming, thus, it is developed by considering some real-life facts. Let me tell you how?

In our society, certain people are categorized in different groups, which we call them as Middle class, Upper middle class, Lower class, Upper class, etc.  People who lie within such groups are the entity of the class. So we derive the concept that people are the physical quantity of the class who are technically called objects, whereas, Classes like Lower, Middle, Upper Middle, and Upper class are just the feeling inside the person. There is no such physical boundary among classes that can be drawn.

The objects and classes in Java work as the above analogy.

Objects and classes in Java

What are Objects in Java?

An object is the physical and logical entity of the class which has state, behavior, and identity. A state is the value of an object, Behavior is the way it responds to any action and Identity means that it has a unique id which JVM uses to differentiate among different objects of the same class. Simply, an object is the instance of the class.

For example, Avinash is the object of Person class. It has the name- Identity. He is fair and kind- State. He is a blogger and he shares the Java & Selenium knowledge- Behavior.

What are the classes in Java?

It is a logical entity that acts as a template for an object. A class in java has items like- Variables, Constants, Constructors, Methods, Nested classes, Interfaces, Blocks, etc.

Instance Variable of the class

An instance variable is the variables created inside the class and outside the method. It gets memory at runtime when an object is created; hence, it doesn’t get memory at compile time.

the new keyword is used to create a memory at runtime, hence, it is used to create a new object as well; which gets stored in heap memory.

Methods of a class

Methods are action items that an object performs. It is used for code reusability and optimization.

How to create an Object in Java?

We can create objects by following ways:

  1. With new keyword
  2. With newInstance() method
  3. With deserialization
  4. With clone() method of Object class
  5. With factory method
  6. With anonymous object creation technique

We will discuss them later while discussing Object class explicitly.

How to create Anonymous Object?

Such objects are nameless objects without any references. They are used instantly while creating an object.

Example: new Person();

Objects and Classes in Java Stack Heap memory

How to initialize Objects of the class in Java?

There are three ways to initialize an object in Java.

  • Through reference
  • Through Method
  • Through Constructor

Object Initialization through Reference

class ObjectByReference{  
	 int id;  
	 String name;  
	}  
	class TestObject{  
	 public static void main(String args[]){  
		 ObjectByReference obj =new ObjectByReference();  
	  obj.id=101;  
	  obj.name="Avinash";  
	  System.out.println(obj.id+" "+obj.name);
	 }  
	}

Object Initialization through Method

class ObjectByReference{  
	 int id;  
	 String name;  
	 
	 public void insertData(int r, String n){  
		  id=r;  
		  name=n;  
		 }  
	 
	 public void displayData(){
		 System.out.println(id+" "+name);
		 }  
	}  
	class TestObject{  
	 public static void main(String args[]){  
		 ObjectByReference obj =new ObjectByReference();  
		 ObjectByReference obj1 =new ObjectByReference();
		 obj.insertData(101,"Avinash");  
		 obj1.insertData(102,"Mishra");  
		 obj.displayData();  
		 obj1.displayData(); 
	 }  
	}  

Object Initialization through Constructor

class ObjectByReference{  
	 int id;  
	 String name;  
	 
	 ObjectByReference(int i, String n){
		 id=i;
		 name=n;
	 }
	 
	 public void displayData(){ 
		 System.out.println(id+" "+name); 
		 } 
	 }
	 
	class TestObject{  
	 public static void main(String args[]){  
		 ObjectByReference obj =new ObjectByReference(101, "Avinash");  
		 ObjectByReference obj1 =new ObjectByReference(102,"Mishra");
		 obj.displayData();  
		 obj1.displayData(); 
	 }  
	}  

Sample Program

class ObjectByReference{  
	 int id;  
	 String name;  
	 
	 ObjectByReference(int i, String n){
		 id=i;
		 name=n;
	 }
	 
	 public void displayData(){ 
		 System.out.println(id+" "+name); 
		 } 
	 }
	 
	class TestObject{  
	 public static void main(String args[]){  
		 ObjectByReference obj =new ObjectByReference(101, "Avinash"), obj1 =new ObjectByReference(102,"Mishra");
		 obj.displayData();  
		 obj1.displayData(); 
	 }  
	}

This was all about Objects and Classes in Java. Next tutorial we are going to discuss Constructors, which is like the second part of this tutorial. Feel free to ask your questions here.

Join Inviul fb group

Leave a Reply