An object is an instantiation of a class. Objects have state and behavior. Variables are used to indicate the state of the object. Methods indicate the behavior of the object. The Life cycle of Java objects includes creation, use, and cleanup, and this article covers object creation in detail
Java Virtual machine memory architecture model in detail
1. Use new to create an object
The new keyword is probably the most common way to create objects, but we should be aware that using new to create objects increases coupling. No matter what framework you use, reduce the use of new to reduce coupling.
public class Hello
{
public void sayWorld()
{
System.out.println("Hello world!");
}
}
public class NewClass
{
public static void main(String[] args)
{
Hello h = new Hello();
h.sayWorld();
}
}
Copy the code
2. Use reflection to create objects
Use the Class newInstance method
The code for the Hello class is unchanged, and the code for the NewClass class is as follows:
public class NewClass { public static void main(String[] args) { try { Class heroClass = Class.forName("yunche.test.Hello"); Hello h =(Hello) heroClass.newInstance(); h.sayWorld(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); }}}Copy the code
Use the Constructor class’s newInstance method
Public class NewClass {public static void main(String[] args) {try {// Get heroClass = Class.forName("yunche.test.Hello"); Constructor = heroclass.getconstructor (); Hello h =(Hello) constructor.newInstance(); h.sayWorld(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }}}Copy the code
3. Using the clone
With Clone, you need to have a source object with memory allocated. When creating a new object, you should first allocate a memory space as large as the source object.
To call the Clone method, you need to implement the Cloneable interface, and since the Clone method is protected, modify the Hello class.
public class Hello implements Cloneable { public void sayWorld() { System.out.println("Hello world!" ); } public static void main(String[] args) { Hello h1 = new Hello(); try { Hello h2 = (Hello)h1.clone(); h2.sayWorld(); } catch (CloneNotSupportedException e) { e.printStackTrace(); }}}Copy the code
4. Use serialization
With serialization, you implement the Serializable interface to serialize an object to disk, whereas with deserialization you convert object information from disk to memory.
public class Serialize { public static void main(String[] args) { Hello h = new Hello(); // Prepare a File to store information about the object File f = new File("hello.obj"); try(FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos); FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis)) {// Serialize object, write oos to disk. WriteObject (h); // Deserialize the object Hello newHello = (Hello)ois.readObject(); Newhello.sayworld (); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }}}Copy the code
Public number [Android development and programming]
Want to know more about Android old iron people, pay attention to!!