Pages

Objects

Object
Definition

Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.


An Objects Contants

State (Instance Variables) : Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.

Behavior (Methods) : When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Identity : An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.


Different Ways to Create Object

Using New Keyword

package com.jst.jaladhi;
public class UsingNewKeyword {
      public String name = "Jaladhi Soft Technology (JST)";
      public static void main(String [ ] args) {
            UsingNewKeyword keyword = new UsingNewKeyword();
            System.out.println(keyword.name);
      }
}

Using newInstance() method of Constructor class

package com.jst.jaladhi;
import java.lang.reflect.Constructor;
public class UsingNewInstance {
      private String name;
      UsingNewInstance() { }
      public void setName(String name) {
            this.name = name;
      }
      public static void main(String [ ] args) {
            try {
                  Constructor<UsingNewInstance> constructor = UsingNewInstance.class.getDeclaredConstructor();
                  UsingNewInstance instance = constructor.newInstance();
                  instance.setName("Jaladhi Soft Technology (JST)");
                  System.out.println(r.name);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}

Using New Instance

package com.jst.jaladhi;
public class UsingNewInstance {
      String name = "Jaladhi Soft Technology (JST)";
      public static void main(String[] args) {
            try {
                  Class classes = Class.forName("UsingNewInstance");
                  UsingNewInstance instance = (UsingNewInstance) classes.newInstance();
                  System.out.println(obj.name);
            } catch (ClassNotFoundException e) {
                  e.printStackTrace();
            } catch (InstantiationException e) {
                  e.printStackTrace();
            } catch (IllegalAccessException e) {
                  e.printStackTrace();
            }
      }
}

Using Clone() Method

package com.jst.jaladhi;
public class UsingClone implements Cloneable {
      String name = "Jaladhi Soft Technology (JST)";
      public static void main(String [ ] args) {
            UsingClone instance = new UsingClone();
            try {
                  UsingClone object = (UsingClone) instance.clone();
                  System.out.println(object.name);
            } catch (CloneNotSupportedException e) {
                  e.printStackTrace();
            }
      }
      @Override
      protected Object clone() throws CloneNotSupportedException {
            return super.clone();
      }
}

Using Deserialization

package com.jst.jaladhi;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class UsingDeserialization implements Serializable {
      private String name;
      UsingDeserialization(String name) {
            this.name = name;
      }
      public static void main(String[] args) {
            try {
                  UsingDeserialization instance = new UsingDeserialization("Jaladhi Soft Technology (JST)");
                  FileOutputStream file = new FileOutputStream("jst.txt");
                  ObjectOutputStream output = new ObjectOutputStream(file);
                  output.writeObject(instance);
                  output.close();
                  file.close();
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}

Different Way to Create Object in Single Class

package com.jaladhi.jst;
import java.io.Serializable;
import java.util.Objects;
public class Person implements Serializable, Cloneable {
      private static final long serialVersionUID = 1L;
      private String name;
      public Person() {
            System.out.println("Calling the Person Normal Constructor.");
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      @Override
      public boolean equals(Object object) {
            if (this == object) {
                  return true;
            }
            if (object == null || getClass() != object.getClass()) {
                  return false;
            }
            Person employee = (Person) object;
            return Objects.equals(name, employee.name);
      }
      @Override
      public int hashCode() {
            return Objects.hash(name);
      }
      @Override
      public String toString() {
            return String.format("Person { Name = '%s' } ", name);
      }
      @Override
      public Object clone() {
            Object obj = null;
            try {
                  obj = super.clone();
            } catch (CloneNotSupportedException e) {
                  e.printStackTrace();
            }
            return obj;
      }
}

package com.jaladhi.jst;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class WaysCreatingObjects {
      public static void main(String [] args) {
            /* 1. Creating Object Using New Keyword. */
            System.out.println("1. Creating Object Using New Keyword.");
            Person person1 = new Person();
            person1.setName("Person 1 : Creating Object Using New Keyword");
            System.out.println(person1 + ", Hash Code : " + person1.hashCode());
            System.out.println();
            /*2. Creating Object Using newInstance() Method in Class class's. */
            System.out.println("2. Creating Object Using New Instance Method in Java Class Clas's.");
            try {
                  Person person2 = Person.class.newInstance();
                  person2.setName("Person 2 : Creating Object Using New Instance Method in Java Class Class");
                  System.out.println(person2 + ", Hash Code : " + person2.hashCode());
            } catch (InstantiationException | IllegalAccessException e) {
                  e.printStackTrace();
            }
            System.out.println();
            /*3. Creating Object Using newInstance() Method in Constructor class's. */
            System.out.println("3. Creating Object Using newInstance() Method in Constructor class's.");
            Constructor constructor = null;
            try {
                  constructor = Person.class.getConstructor();
            } catch (NoSuchMethodException | SecurityException e) {
                  e.printStackTrace();
            }
            Person person3 = null;
            try {
                  person3 = constructor.newInstance();
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                  e.printStackTrace();
            }
            person3.setName("Person 3 : Creating Object Using New Instance Method in Constructor Class");
            System.out.println(person3 + ", Hash Code : " + person3.hashCode());
            System.out.println();
            /*4. Creating Object Using clone() Method. */
            System.out.println("4. Creating Object Using clone() Method.");
            Person person4 = (Person) person3.clone();
            person4.setName("Person 4 : Creating Object Using Clone Method");
            System.out.println(person4 + ", Hash Code : " + person4.hashCode());
            System.out.println();
            /*5. Creating Object Using Serialization and Deserialization */
            System.out.println("5. Creating Object Using Serialization and Deserialization. ");
            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("jst.txt"))) {
                  out.writeObject(person4);
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }
            Person person5 = null;
            try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("jst.txt"))) {
                  person5 = (Person) in.readObject();
                  person5.setName("Person 5 : Creating Object Using Deserialization");
                  System.out.println(person5 + ", Hash Code : " + person5.hashCode());
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            } catch (ClassNotFoundException e) {
                  e.printStackTrace();
            }
            System.out.println();
      }
}
Output :
1. Creating Object Using New Keyword.
Calling the Person Normal Constructor.
Person { Name = 'Person 1 : Creating Object Using New Keyword' } , Hash Code : 777643214
2. Creating Object Using New Instance Method in Java Class Clas's.
Calling the Person Normal Constructor.
Person { Name = 'Person 2 : Creating Object Using New Instance Method in Java Class Class' } , Hash Code : 1520804765
3. Creating Object Using newInstance() Method in Constructor class's.
Calling the Person Normal Constructor.
Person { Name = 'Person 3 : Creating Object Using New Instance Method in Constructor Class' } , Hash Code : 502487934
4. Creating Object Using clone() Method.
Person { Name = 'Person 4 : Creating Object Using Clone Method' } , Hash Code : -838728554
5. Creating Object Using Serialization and Deserialization.
Person { Name = 'Person 5 : Creating Object Using Deserialization' } , Hash Code : 136540156