Back
There are five ways to create an object in java:
By using new keyword:
className object=new className();
By using Class.forName() method:
className object=(className)Class.forName(“className”).newInstance();
By using clone method:
className object =className.clone();
By using Constructor.forName() method:
className object= className.class.getConstructor().newInstance();
By using Deserialization:
ObjectInputStream ois=new ObjectInputStream(inputStream );ClassName object=(MyObject) ois.readObject();
ObjectInputStream ois=new ObjectInputStream(inputStream );
ClassName object=(MyObject) ois.readObject();
Want to learn Java? Check out the Java certification from Intellipaat.
31k questions
32.8k answers
501 comments
693 users