What is Object and Java Object?
The object has state, identity, and behavior in the real world e.g. bike, orange, pen, car, etc. Where in the programming world an object is represented real entities.
Class Object
The object is topmost or root of the class hierarchy in java. Every class has an Object as a Superclass.
Since: JDK1.0, the object class is the root class of every Java class.
Characteristics of an Object:
- State: represents the data (value) of an object.
- Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
- Identity: An Object with a unique name in programming it’s Unique ID.
Example of an object: dog
- Identity: Name of dog
- State or Attribute: color, size
- Behavior: Bark, bite
Syntax:
Using new
keyword → constructor gets called, Where Employee is a class.
Employee emp1 = new Employee();
Java object example:
Here are three steps to creating a Java object:
- Declaration of the object
- Instantiation of the object
- Initialization of the object
new
keyword – The new keyword is used to allocate memory at runtime. All objects get memory in Heap memory area.
Create student.java class
class Student{ int id; String name; }
Then Create another class Hello.java with the main method.
class Hello{ public static void main(String args[]){ // Creating object s1 Student s1=new Student(); // get the values form student class System.out.println(s1.id); System.out.println(s1.name); } }
Program screen and Output:
Java object clone
You can do copy same java object or cloning by using clone() method in java. The value of fields have saved with the same class will copy.
For this, you have to add Parameter constructor’s and implement a Cloneable interface with the clone method, here is code.
class Student implements Cloneable{ int id; String name; Student(int id, String name) { this.id = id; this.name = name; } public Object clone() throws CloneNotSupportedException { return super.clone(); } }
Then when creating a second object, that time you can clone first object values. It will copy all thing same, here is code. We used try-catch exception handling, don’t worry later chapter will discuss all terms.
class Hello { public static void main(String args[]) { // Creating object s1 Student s1 = new Student(1, "Joy"); // get the values form student class System.out.println(s1.id + " " + s1.name); try { Student s2 = (Student) s1.clone(); System.out.println(s2.id + " " + s2.name); } catch (CloneNotSupportedException c) { System.out.println(c); } } }
Output: 1 Joy
1 Joy
Java object array
You can also create an Object array like that.
A[] a = new A[4];
You couldn’t do a1.AnyMethod() without allocating a1 as
a1 = new A();
similarly, with the array, you need to do.
a[0] = new A();
QA: What is the parent class of all classes in Java?
java.lang.Object
.
The Java object class is a root or parent or base class and a superclass of every class.
Let’s test it, creating a bulb class and check it.
public class Hello { public static void main(String[] args) { Bulb a = new Bulb(); if (a instanceof Object) { System.out.println("Object is a superclass of all classes"); } } }
Output: Object is a superclass of all classes
QA: What is the need for cloning an object in Java?
If your application has a cloned copy of something means, you have “before” and “after” versions. So you can leave the original copy of object alone while you testing with a copy object. You can provide a undo or revert functionality in the application.
Example: If you are doing the transaction and it’s successful then you can check the step results.
Do comment if you have any doubt and suggestions on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1Java version 11
All Java Object Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.