2

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?

5
  • 3
    No, a constructor does not create an instance. A constructor initializes an instance. Commented 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? Commented Mar 25, 2015 at 2:47
  • @SotiriosDelimanolis What is the difference between creating and initializing? Commented 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). Commented 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. Commented Mar 25, 2015 at 3:12

4 Answers 4

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

Constructors don't create objects. They merely initialize objects(and their data members) once they are created using parameters(when provided) or to default values.

Comments

0

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

0

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).

  1. 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.
  2. 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.
  3. Whenever we create a new object, we have to use new keyword, 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.