1.常见的使用new
User user = new User();
2.使用Class的newInstance
User user = (User) Class.forName("com.entity.User").newInstance();
或
User user = User.class.newInstance();
3.Constructor类的newInstance
Constructor<User> constructor = User.class.getConstructor();
User user = constructor.newInstance();
4.使用clone
User user = (User) user1.clone();
5.使用序列化与反序列化,对象要实现Serializable接口
Jay j = Jay.class.newInstance();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
out.writeObject(j);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Jay j2 = (Jay) in.readObject();
in.close();