I recently talked with my colleagues about how to create objects. Most people use constructors to create objects, but all of a sudden, I wondered if there was any other way. What are some ways to create objects?
java
First of all, there is a method using the new keyword.
This is the most common way, and almost all objects are created like this.
MyObject object = new MyObject();
The second method is to write Class.forName() If we know the name of the class and the class has a public default constructor defined, we can create it this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
Third, it is a method using clone(). If an object already exists, it is created by copying it.
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();
The fourth is the object designation method.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
This is how serialization is used to create objects.
546 Understanding How to Configure Google API Key
534 Uncaught (inpromise) Error on Electron: An object could not be cloned
547 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
547 Who developed the "avformat-59.dll" that comes with FFmpeg?
703 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.