Creating and Operating Objects in Java
How to Create and Work with Objects in Java ?
In this article we will be discussing about Creating and Operating objects in Java.
Java is an object oriented programming language. Java treats everything as objects and classes. objects are created using class name.
Creating Objects
We can create an object of a class by using:
- new keyword
- clone() method
- deserialization
- new instance
- newInstance() method of Constructor class
Creating an object using new keyword:
The most typical method of creating an object is by using new keyword. The new keyword creates a reference to the object and allocates memory for it.The new keyword calls the constructor of the class at the time of object creation.
import java.util.*; // class definition class prepinsta{ public String name; //constructor prepinsta(){ name = "Lakshit"; } } class Main{ public static void main(String[] args){ // Object creation using new keyword prepinsta obj = new prepinsta(); System.out.println(obj.name); } }
Output: Lakshit
Creating an object using clone() method:
The Clone() method makes a duplicate object of an existing object. We can’t create any new Objects by using the clone() function. The clone() function generates an object of a given class without calling any of the class constructors
import java.util.*; class prepinsta implements Cloneable{ String name = "Lakshit"; int age = 21; public static void main(String[] args){ prepinsta obj1 = new prepinsta(); try{ prepinsta obj2 = (prepinsta)obj1.clone(); System.out.println(obj2.name); System.out.println(obj2.age); } catch (Exception e) { System.out.println(e); } } }
Output: Lakshit 51
Creating an object using deserialization:
When we serialize and subsequently deserialize an object, java virtual machine creates an object for the class. VM doesn’t use any constructor to create the object.
import java.io.*; class prepinsta implements Serializable { private String name; prepinsta(String name){ this.name = name; } public static void main(String[] args){ try{ prepinsta obj = new prepinsta("Lakshit"); FileOutputStream file = new FileOutputStream("file.txt"); ObjectOutputStream out = new ObjectOutputStream(file); out.writeObject(obj); out.close(); file.close(); } catch (Exception e) { e.printStackTrace(); } } }
Output: Lakshit
Creating an object using new instance:
If the class has a public default constructor in it, we can generate an object with the Class.forName method. It can be used to construct Class Objects. Class.forName doesn’t actually generate any Objects; instead, it simply loads the Class in Java.
import java.io.*; class prepinsta{ public String name; prepinsta(){ name = "Lakshit"; } public static void main(String[] args){ try { Class prep = Class.forName("prepinsta"); prepinsta insta = (prepinsta) prep.newInstance(); System.out.println(insta.name); } catch (Exception e){ System.out.println(e); } } }
Output: Lakshit
Creating an object using newinstance() method of constructor class:
With the exception of the ability to pass parameters for various parameterized constructors, the newInstance() function of the Constructor class is comparable to the newInstance() method of the Class class.
import java.lang.reflect.*; class prepinsta{ private String name; prepinsta(){} public void setName(String name){ this.name = name; } public static void main(String[] args){ try { Constructorconstructor = prepinsta.class.getDeclaredConstructor(); prepinsta prep = constructor.newInstance(); prep.setName("Lakshit"); System.out.println(prep.name); } catch (Exception e) { e.printStackTrace(); } } }
Output: Lakshit
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment