Is a constructor part of a class or part of an object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is a constructor part of a class or part of an object?

Am I right with my assumption, that a constructor is part of an object, not of a class? In a class we only define, that there's a constructor (and how it behaves) for each instantiated object of that class. That means, that the useable part of the constructor is located in the object, not in the class.

3rd Jan 2019, 11:18 PM
Dominik Wolf
Dominik Wolf - avatar
13 Answers
+ 16
D_Stark ๐Ÿ‘ Thank you so much! ๐Ÿ˜Š When we learn something, then we have the goal to understand it, first, I try to explain to myself and when everything is crystal clear,๐Ÿ‘Œ then I can transfer my knowledge to others! NOTE: that reference variable never contains object directly. ** It contains address which points to data stored in the memory location. Dominik Wolf You can see, in the example, that when we did not initialize the values of the instance variable in the constructor, then ใ€‹defaultใ€Š values are stored on the heap after calling constructor, but, when we initialize the values in the constructor (parameterized), then after calling the constructor, default values will be eleminated and initialized values stored in the memory location of heap.
4th Jan 2019, 8:40 AM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 13
** When we create the object then the constructor will be automatically called by the JVM. ** If you don't define any constructor, the compiler automatically creates a default constructor and assign default values for all your variables in the class. public class Person{ ย  // Declaration of Instance variables. ย  String name; ย  int age; ย  String address; ย  // Here, We are not creating any constructor. So Compiler will automatically insert the default constructor. ย  // Create one method to print the default values. ย  void display(){ ย ย ย  System.out.println(name +" "+ age +" "+ address); ย  } ย  public static void main(String[] args){ ย ย ย  // Create the object of the class using new keyword. ย ย ย  Person p = new Person(); ย ย ย  // Call the method using object reference variable p. ย ย ย  p.display(); ย  } } Output: null 0 null ** Constructor is used to initialize the state of an object whereas Method is used to expose the behaviour of an object.
4th Jan 2019, 6:47 AM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 11
Dominik Wolf I do not think you read my answer well, I would like someone to correct me if I'm wrong! The purpose of the constructor is not to make objects, that's right (I wrote it allows). โ€ข When you create an object, its constructor is called before any methods. โ€ข When you dont't define a constructor, Java provides its own default constructor. โ€ข If you define your own constructor then you have to decide what values should be given to the instance variables. โž Constructor is used to assign the deafult values of instance variables.
4th Jan 2019, 6:43 AM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 10
โ€ข A constructor is a special method whose name is same as that of the class name and it is called when you use the 'new' operator. Consider the following example : ย  ย  ย  ย  ย  ย ย  School sc = new School(); Where, ย  ย  ย  ย  ย  ย  ย School โž name of the class. ย  ย  ย  ย  ย  ย  ย  ย  sc ย  ย  โž Object reference variable which stores the address of the object in the stack memory. ย  ย  ย  ย  ย  School() โž Constructor of the class. ย  ย  ย  ย  ย  ย  ย new ย  ย  โž is a special keyword that allocatesย the memory to store the object whose type is specified by a constructor and then calls the constructor to initialize the object, which is stored in the heap (a region of memory for storing objects). When constructor ends, new return memory addresses to the object so that it can be accessed from anywhere in the application. ** A constructor in Java is used to perform initialization of instance variables of a class.
3rd Jan 2019, 11:43 PM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 10
Dominik Wolf I also want to understand all this and therefore I am trying to find the answer. I do not know how much it affects, we come with different languages, but I think the essence is the same. LukArToDo, John Wells, David Carroll I will appreciate every help! ๐Ÿ‘ Thanks ๐Ÿ˜Š
4th Jan 2019, 7:02 AM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 9
Constructor in Java โ€ขย When you create an object of a class using the 'new' operator, you invoke (calls) a special kind of method called a constructor just after the memory allocated for that object. In other words, we can also say that : ** Constructor in Java is a block of code within a class that allows creatingย object of the class.
3rd Jan 2019, 11:37 PM
Danijel Ivanoviฤ‡
Danijel Ivanoviฤ‡ - avatar
+ 9
Yeah your right, the class is just a template for creating objects the constructor of each object is unique to the object its self and yes its used to initialize variables. This started to make more sense to me when i started learning javascript i know these are 2 diffrent languages but they do share some similarities and its somthing you need to know when working with oop.
4th Jan 2019, 12:16 AM
D_Stark
D_Stark - avatar
+ 8
Dominik Wolf Btw a easier way to understand whats happening under the hood is when you create an object/instance of class you will see some codes using the keyword "this" which refers to the refrenece to the current object thats been invoked which shows that each time you create an object/instance of class each object will have its own refrence in heap memory which is unique to each object even if there was 10 instances of the same class each instance will call its own constructor to initialize its variables. Danijel Ivanoviฤ‡ explained this very well the constructor is used in a way to bring life to the object by initializing variables of the new object hope that clears any confusement ๐Ÿ‘
4th Jan 2019, 8:05 AM
D_Stark
D_Stark - avatar
+ 7
Guys, i think that a read here is helpful if any doubts https://www.javaworld.com/article/2076614/core-java/object-initialization-in-java.amp.html ๐Ÿ˜‰
4th Jan 2019, 8:36 AM
KrOW
KrOW - avatar
+ 6
There is only one copy of the code for all methods (both static class based or object instance based including the constructor and destructor.) The instance based methods all have an extra parameter added to pass the instance (this, self, excetera.) This makes these methods part of the class not the object. To see the underlying implementation of classes and how they were coded prior to language support, please see my C code here: https://code.sololearn.com/cg2MNAExxvQ6 The corresponding C++ class is here: https://code.sololearn.com/c373AHt3O2jy
4th Jan 2019, 4:11 PM
John Wells
John Wells - avatar
+ 3
So we can say, that the constructor is encapsulated within an object, and each object has its own instance of a constructor?
3rd Jan 2019, 11:33 PM
Dominik Wolf
Dominik Wolf - avatar
+ 2
Danijel Ivanovic I don't think that's true. The purpose of a constructor is not for creating objects. Its main purpose is to initialise the attributes of an object. It's true though, that every time an object is created, a constructor will be called (if there's no user-defined constructor, it will call a default constructor (myclass() {}).
3rd Jan 2019, 11:50 PM
Dominik Wolf
Dominik Wolf - avatar
+ 2
Danijel Ivanoviฤ‡ no props friend ๐Ÿ˜‰๐Ÿป๐Ÿ˜
4th Jan 2019, 11:04 AM
D_Stark
D_Stark - avatar