In Java, does the constructor of a class create an instance of that class? And if it does, does it also initialize the variables of that class?
-
3No, a constructor does not create an instance. A constructor initializes an instance.Sotirios Delimanolis– Sotirios Delimanolis2015-03-25 02:46:45 +00:00Commented Mar 25, 2015 at 2:46
-
Thank you! I tried reading the documentation, but it wasn't clear to me. Do you happen to know any good sources for learning the basics about constructors?andrejon– andrejon2015-03-25 02:47:54 +00:00Commented Mar 25, 2015 at 2:47
-
@SotiriosDelimanolis What is the difference between creating and initializing?yitzih– yitzih2015-03-25 03:06:31 +00:00Commented Mar 25, 2015 at 3:06
-
@yitzih Creating is the process of allocating memory for an object. It is done through a new instance creation expression. Initializing is the process of preparing the state of an object (ex, assigning values to its fields).Sotirios Delimanolis– Sotirios Delimanolis2015-03-25 03:11:39 +00:00Commented Mar 25, 2015 at 3:11
-
I believe creating would involve allocating the memory, whereas initializing would involve setting the instance variables. The constructor is only responsible for the second part in Java.Ron Thompson– Ron Thompson2015-03-25 03:12:20 +00:00Commented Mar 25, 2015 at 3:12
4 Answers
Constructor doesn’t create the instance of the Class.
Instance creation is done using either:
1.Using Class.forName()
2.ClassLoader loadClass()
3.Using clone()
4.Deserialization
5.Using reflection
6.new keyword
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
1.Constructor name must be same as its class name
2.Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
1.Default constructor (no-arg constructor)
2.Parameterized constructor
Comments
When you create an instance of the class using new operator, the constructor of the class is called so as to initialize the instance variables. If the constructor defined is default, then instance variables have to be assigned to the newly created object explicitly. However when you override a constructor using fields, then the instance variables for that newly created object are assigned during object creation.
Comments
I would love to explain this in a very simple language. In the real-world to build something, we need two things first is its prototype/model, and the second is someone who can create it based on that prototype. A very relevant simple example is to build a house, you first need its blueprint(map), then a constructor who can build it based on that blueprint. So, similarly in the programming language
Object: A real-world entity for which we create a class.
Class: A class describes the "blueprint" of the objects that are made out of it (are instances of it).
- For software development, we first have to think about the objects(any real-world entity), then we create a class (blueprint) for it, which contains its attributes.
- After creating a class when we need to create one or more objects based on it, for this, we need a constructor to build it.
- Whenever we create a new object, we have to use
newkeyword, and it tells the constructor to create the object.
When you are initialing variables in a class they are just part of the blueprint, based on that, the object will be created. So, without a constructor, you cannot create new objects, but there are some exceptional cases and tricks where you can create them without calling constructors.