+ 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.
13 Antworten
+ 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.
+ 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.
+ 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.
+ 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.
+ 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 😊
+ 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.
+ 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.
+ 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 👍
+ 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
😉
+ 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
+ 3
So we can say, that the constructor is encapsulated within an object, and each object has its own instance of a constructor?
+ 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() {}).
+ 2
Danijel Ivanović no props friend 😉🍻😁