There are four different ways to create objects in java:

A. Using new keyword
This is the most common way to create an object in java. Almost 99% of objects are created in this way.

 MyObject object = new MyObject();

B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

C. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

You can read them from here.

Answer from kamaci on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › objectcreation.html
Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
If you inspect the code within the constructor, you will see that it creates a new Point object whose x and y values are initialized to 0: ... The Rectangle constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor: ... All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor.
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-create-objects-java
Different Ways to Create Objects in Java - GeeksforGeeks
March 28, 2025 - Example: This example demonstrates how to create a copy of an object in Java using the clone() method.
Discussions

What are all the different ways to create an object in Java? - Stack Overflow
It would make more sense if you actually give some exampls 2018-05-26T03:53:25.153Z+00:00 ... Save this answer. ... Show activity on this post. ... Nobody has discuss it. ... This is the way of creating primitive data types, it is just a flexibility that Java provides behind the scenes to not use the "new" keyword.This is the same as the new keyword. 2014-06-24T07:04:06.533Z+00:00 ... Madhusudan, FYI , With the help of new operator, objects ... More on stackoverflow.com
🌐 stackoverflow.com
FastAPI best practices
If the dependant knows the function that instantiates the dependency, then there is no inversion of control whatsoever. it sounds like you are still looking at this with a java mental model there is definitely IoC here; resolving dependencies and deciding how and when to create the objects and how to wire them is still happening externally by the framework, but unlike java way of doing things (global container registry, reflection, type-driven DI), it's just done in more pythonic way following "zen" philosophy: explicit is better than implicit you declare what you need. fastapi will inspect function signatures, sees Depends, works out all sub-dependencies to resolve a dependency graph, handles scope (per request) and caching as needed, and use that to decide when to create it, in what order, where to injecting it, and when to clean it up. it might feel less "pure" for someone coming from Java background, but this is indeed IoC; fastapi DI is function-centric, more explicit, and less magical. It's a design choice :) plus tight coupling and testing is not an issue either, dependencies_overrides allows you to replace nodes in the dependency graph at any stage. More on reddit.com
🌐 r/FastAPI
38
41
March 7, 2026
Is it possible to create object of an object in Java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
4
2
October 5, 2022
The argument 'self'
Because method time_explanation() is defined only once, on the class level - while property dog_time_dilation is separately stored for each object/instance of this class - Rocky, Rex, Clifford. You need to instruct time_explanation() on which specific dog should it be launched. It is true that in many programming languages this is simplified by hiding self in method signatures, and allowing direct access to instance variables (even without prefixing them with this.). But internally, they do pass self just like Python. More on reddit.com
🌐 r/learnpython
37
35
March 30, 2026
🌐
W3Schools
w3schools.com › java › java_classes.asp
Java Classes and Objects
The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects. To create a class, use the keyword class.
🌐
Scientech Easy
scientecheasy.com › home › blog › how to create object in java
How to Create Object in Java - Scientech Easy
April 2, 2026 - The class name followed by parentheses represents the constructor of the class. For example, ... This statement tells the JVM to allocate memory space for a new College object on the heap. Now, we will link the object and reference created above like this: // This is a general syntax for creating an object of any class in Java.
🌐
JanbaskTraining
janbasktraining.com › home › how to create object in java with examples?
How to Create Object in Java with Examples? Methods to Create Object
December 14, 2018 - This call will initialize a new object too. Java developers are generating a plenty of objects daily but they are based on new and dependency management systems. For example, spring can be used to create objects here.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › objectcreation.html
Creating Objects
If you inspect the code within the constructor, you will see that it creates a new Point object whose x and y values are initialized to 0: Rectangle rectTwo = new Rectangle(50, 100); The Rectangle constructor used in the following statement doesn't take any arguments, so it's called a no-argument ...
🌐
Medium
medium.com › javarevisited › ways-to-create-objects-em-java-679e4f3c2160
Ways to create objects in Java with Builder and Factory | by Marcos | Javarevisited | Medium
January 10, 2022 - It’s relatively simple, we need a class whose only function is to provide the creation of an object: import java.time.LocalDate;public class DogFactory { public Dog create(String name, LocalDate birthDate, String breed, String sex, Double ...
Find elsewhere
Top answer
1 of 16
294

There are four different ways to create objects in java:

A. Using new keyword
This is the most common way to create an object in java. Almost 99% of objects are created in this way.

 MyObject object = new MyObject();

B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

C. Using clone()
The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

You can read them from here.

2 of 16
71

There are various ways:

  • Through Class.newInstance.
  • Through Constructor.newInstance.
  • Through deserialisation (uses the no-args constructor of the most derived non-serialisable base class).
  • Through Object.clone (does not call a constructor).
  • Through JNI (should call a constructor).
  • Through any other method that calls a new for you.
  • I guess you could describe class loading as creating new objects (such as interned Strings).
  • A literal array as part of the initialisation in a declaration (no constructor for arrays).
  • The array in a "varargs" (...) method call (no constructor for arrays).
  • Non-compile time constant string concatenation (happens to produce at least four objects, on a typical implementation).
  • Causing an exception to be created and thrown by the runtime. For instance throw null; or "".toCharArray()[0].
  • Oh, and boxing of primitives (unless cached), of course.
  • JDK8 should have lambdas (essentially concise anonymous inner classes), which are implicitly converted to objects.
  • For completeness (and Paŭlo Ebermann), there's some syntax with the new keyword as well.
🌐
Scaler
scaler.com › home › topics › how to create object in java?
How to Create Object in Java? - Scaler Topics
April 7, 2024 - ... To use this object creation method, you need to know the class name and ensure that the class has a public default constructor. Use Class.forName(<class_name>) to load the Java class.
🌐
TechVidvan
techvidvan.com › tutorials › java-object-creation
Java Object Creation - Learn to Create Objects with Different Ways - TechVidvan
April 6, 2020 - By using the new keyword, we can call any type of constructor of the class that is, either the parameterized constructor or non-parameterized constructor. Dive a little deep into the concept of Java Constructor with Techvidvan.
🌐
Dev.java
dev.java › learn › creating-and-using-objects
Creating and Using Objects
Introducing the object oriented programming. ... Getting to know the basics of the Java language. ... Defining your own classes, declaring member variables, methods, and constructors. ... How to model your immutable data with records to make your code simpler and more readable. ... Understanding numbers, characters and strings of characters. ... Leveraging inheritance in Java applications. ... Creating ...
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 99wi › java › tutorial › java › javaOO › objectcreation.html
Creating Objects
Date today = new Date(); This statement creates a new Date object (Date is a class in the java.util package). This single statement actually performs three actions: declaration, instantiation, and initialization. Date today is a variable declaration which simply declares to the compiler that ...
🌐
Baeldung
baeldung.com › home › java › core java › different ways to create an object in java
Different Ways to Create an Object in Java | Baeldung
January 8, 2024 - Using the new keyword is probably the most common way to create an object: ... In the example above, we assign a new instance of a Rabbit to a variable named rabbit. The new keyword indicates that we want a new instance of the object.
🌐
DZone
dzone.com › coding › java › five different ways to create objects in java
Five Different Ways to Create Objects in Java
September 9, 2020 - I have covered cloning in detail in a 3 article long, Java Cloning Series. Please go ahead and read them if you want to know more about cloning. Whenever we serialize and then deserialize an object, JVM creates a separate object for us. In deserialization, JVM doesn’t use any constructor to create the object.
🌐
Javatpoint
javatpoint.com › how-to-create-object-in-java
How to Create Object in Java - javatpoint
How to Create Object in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, inheritance, array, string, map, math, methods, examples etc.
🌐
Florida State University
cs.fsu.edu › ~jtbauer › cis3931 › tutorial › java › javaOO › objectcreation.html
Creating Objects
Often, you will see a Java object created with a statement like the following, which creates a new Rectangle object from the Rectangle class given in the previous section: Rectangle rect = new Rectangle(); This single statement performs three actions: Declaration: Rectangle rect is a variable ...
🌐
Mysoftkey
mysoftkey.com › java › 4-ways-to-create-object-in-java
4 Ways to create object in Java – mySoftKey
There are 4 different ways to create object in Java. Learn different ways by example: using new keyword, using Class.forName, cloning and deserializing.
🌐
Tutorialspoint
tutorialspoint.com › java › java_object_classes.htm
Java - Classes and Objects
Consider the below syntax to create an object of the class in Java: Class_name object_name = new Class_name([parameters]); Note: parameters are optional and can be used while you're using constructors in the class.