Ways to Create Object
About
1. Using the new Keyword:
new Keyword:// Using the new keyword
MyClass obj = new MyClass();2. Using Reflection:
try {
Class<?> clazz = Class.forName("MyClass");
MyClass obj = (MyClass) clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}3. Using the clone() Method:
clone() Method:4. Using Deserialization
5. Using Factory Methods:
6. Using Singleton Pattern:
7. Using Builder Pattern
Last updated