1. new
2. clone()
3. 通过反射机制创建
4. 序列化反序列化
反射示意代码:// Class.forName方法获取类,在调用类的newinstance()方法 Class cls = Class.forName("com.dao.User"); User u = (User)cls.newInstance();
序列化示意代码:
//将一个对象实例化后,进行序列化,再反序列化,也可以获得一个对象(远程通信的场景下使用) ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream("D:/data.txt"));
//序列化对象 out.writeObject(user1); out.close();
//反序列化对象 ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/data.txt")); User user2 = (User) in.readObject(); System.out.println("反序列化user:" + user2); in.close();