HTML Java

Java Object


5 Different ways to create an Object


1) Using new keyword

Student.java

public class Student {
String str;
public Student(String string) {
str = string;
System.out.println(str);
}
}

ObjectCreationUsingNewKeyword.java

public class ObjectCreationUsingNewKeyword {
public static void main(String[] args) {
String str = "Student Object is created using new Keyword";
Student s = new Student(str);
}
}

Output:

Student Object is created using new Keyword


2) Using Class.newInstance()

Student.java

public class Student {
public Student() {
System.out.println("Student Constructor using Class.newInstance()");
}
}

ObjectCreationUsingNewInstance.java

public class ObjectCreationUsingNewInstance {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Class cls = Class.forName("Student");
Student s =(Student)cls.newInstance();
}
}

Output:

Student Constructor using Class.newInstance()


3) Using Deserialization

Employee.java

import java.io.Serializable;
public class Employee implements Serializable{
int salary;
String address;
public Employee() {
}
public Employee(int salary,String address) {
this.salary = salary;
this.address = address;
}
@Override
public String toString() {
return "Employee [salary=" + salary + ", address=" + address + "]";
}
}

ObjectCreationUsingDeserialization.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
public class ObjectCreationUsingDeserialization {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException, ClassNotFoundException {
// Serialization
FileOutputStream fos = new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee e = new Employee(3000,"Lucknow");
oos.writeObject(e);
//Deserialization FileInputStream fis = new FileInputStream("employee.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Employee e1 = (Employee) ois.readObject();
System.out.println(e1);
}
}


4) Using Constructor newInstance()

Student.java

public class Student {
public Student() {
System.out.println(" Object is created Using Constructor.newInstance()");
}
}

ObjectCreationUsingConstructorNewInstance.java

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ObjectCreationUsingConstructorNewInstance {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = Student.class.getConstructor();
Student s = constructor.newInstance();
}
}

Output:

Object is created Using Constructor.newInstance()


5) Copy Clone

i) Using assignment of Reference variable

Employee.java

import java.io.Serializable;
public class Employee implements Serializable{
int salary;
String address;
public Employee() {
}
public Employee(int salary,String address) {
this.salary = salary;
this.address = address;
}
@Override
public String toString() {
return "Employee [salary=" + salary + ", address=" + address + "]";
}
}

ObjectCloningUsingCopyOfRefernceVariable.java

public class ObjectCloningUsingCopyOfRefernceVariable {
public static void main(String args[]) {
// using assignment Operator to create object
Employee e = new Employee(1000,"Iran");
Employee e2 = e;
// anything we will change in e2 reflect in e
//System.out.println(e2);
e.address ="Mumbai";
System.out.println(e2);
System.out.println(e);
}
}

Output:

Employee [salary=1000, address=Mumbai]
Employee [salary=1000, address=Mumbai]


ii) Using clone() method for shallowing copy

ObjectCloningUsingCloneMethod.java

public class ObjectCloningUsingCloneMethod {
public static void main(String[] args) throws CloneNotSupportedException {
Employee e = new Employee(1000,"India");
Human h = new Human(1,1,e);
//System.out.println(h);
Human h1 = (Human)h.clone();
h1.legs = 2;
System.out.println(h);
System.out.println(h1);
// change in object type field
h1.e.salary = 2000;
h1.e.address = "America";
System.out.println(h);
System.out.println(h1);
}
}

Human.java

public class Human implements Cloneable{
int ears;
int legs;
Employee e;
public Human(int ears, int legs,Employee e) {
super();
this.ears = ears;
this.legs = legs;
this.e = e;
}
public Object clone() throws
CloneNotSupportedException
{
return super.clone();
}
@Override
public String toString() {
return "Human [ears=" + ears + ", legs=" + legs + ", e=" + e + "]";
}
}

Output

Human [ears=1, legs=1, e=Employee [salary=1000, address=India]]
Human [ears=1, legs=2, e=Employee [salary=1000, address=India]]
Human [ears=1, legs=1, e=Employee [salary=2000, address=America]]
Human [ears=1, legs=2, e=Employee [salary=2000, address=America]]