In the real world, every entity can be considered an object. In Java, an object is an instance of a class. Every object is a combination of states and behavior. A class defines the structure of an object.
A class serves as a blueprint for objects, determining how the object will look and behave. First, we define a class with its states and behavior, and then we can create multiple objects based on that class.
If you don’t know what is a class then please check the article – What Is Class In Java With Example?
Once a class is defined, we can create multiple objects (instances) of that class. Each object has its own separate copy of the instance variables (states) and can call the class methods to perform actions on these states. If the state of one object changes, it will not affect the state of another object.
How to create an object in Java?
Suppose we have one class named Rectangle for understanding the concept of Object.
In Java, the new
keyword is used to create an object. It dynamically allocates memory for the object on the heap, and after the object is created, a reference to that memory is assigned to the reference variable.
Syntax:
ClassName referenceVariable = new ClassName();
Diagram:
In this diagram, myRect
is a variable of type Rectangle
. Rectangle
is the name of the class. First, we declare the reference variable myRect
on the left side. The new
keyword is used to create an object and Rectangle()
is calling the constructor of the Rectangle
class. The constructor’s name is the same as the class name.
In Java, when you create an object using the new
keyword, the object itself is created in the heap memory. However, what gets stored in the reference variable is not the object itself, but a reference (or a pointer) to the location where the object is stored in memory.
The reference variable holds the memory address (reference) to the object, allowing you to access and manipulate the object indirectly.
In Java, when you create an object using the new
keyword, the Java Virtual Machine (JVM) allocates memory for that object on the heap.
When you print myRect
, which is an instance of the Rectangle
class, and see an output like Rectangle@5c647e05
, it represents the following:
- Class Name:
Rectangle
is the name of the class to which the object belongs. This indicates the type of the object you’re dealing with. - “@” Symbol: The “@” symbol is used as a separator between the class name and the hash code.
- Hash Code:
5c647e05
is the hash code of the object in hexadecimal format. This hash code is a representation of the object’s memory address, though it is not the actual memory address itself. The JVM generates this hash code using theSystem.identityHashCode()
method, which is derived from the memory address allocated for the object in the heap.
If we only declare a variable myRect
of Rectangle
type and do not create an object, then the value of myRect
is null
because it does not point to any object and without initialization, the default value is null.
Rectangle myRect; // Declaration only
System.out.println(myRect); // This will print 'null'
Creating an object from a class is also known as instantiation. Objects are also called instances.
Similar examples of creating objects in Java.
Person p1 = new Person();
Student s1 = new Student();
Car c1=new Car();
Conclusion
In summary, objects are essential in Java, serving as instances of classes that combine data and behavior. They enable efficient organization of code and allow for the implementation of object-oriented programming principles, making software development more intuitive and scalable.
For more information on objects in Java, you can refer to the following resources:
We’d Love to Hear From You!
Do you have questions or feedback about objects in Java? We’re here to help! Whether you need further clarification, have suggestions, or just want to share your thoughts, please leave a comment below. Your feedback is invaluable in helping us improve our content and support others. Don’t hesitate to reach out—we’re excited to hear from you!