No, a constructor is not the same as "the name of the class".
A constructor is a special block of code that is executed when you create a new object from the class. A class can have multiple constructors, with different parameters.
I will look at the tutorial. Thank you. I struggle to understand how mysterious this seems. A constructor is the name of the class, but is not the class name? That is confusing. I thought an official constructor is something like
It looks to me like Greeting is the name of the class and Greeting is the name of the constructor. Are they not the same? How do they differ?
I understand if one is not explicitly stated it is implicitly understood.
I should not be so caught up on this, but I did not do well on an interview because I got really flustered with this.
David Starr wrote:I will look at the tutorial. Thank you. I struggle to understand how mysterious this seems. A constructor is the name of the class, but is not the class name? That is confusing. I thought an official constructor is something like
It looks to me like Greeting is the name of the class and Greeting is the name of the constructor. Are they not the same? How do they differ?
A class in terms of object oriented paradigm represents a class of objects. For example, Car, Animals, Furniture etc represent classes of cars, animals, furniture etc. A constructor, as its name suggests, is a member of the class is used to construct objects of the class. Its purpose is simple. It is used to initialize the values of the properties of the objects of the class. So, for example, the Car class may be thought of as follows:
So when you say :
,
it creates an Object of class Car and initializes its properties to the passed values. That is what a constructor is meant for. It is a rule that a constructor should have the same name as the class itself and should not have any return type. Have you understood? Or do you still have doubts?
David Starr wrote:I will look at the tutorial. Thank you. I struggle to understand how mysterious this seems. A constructor is the name of the class, but is not the class name? That is confusing.
The apparent conflict there stems from the ambiguity of some English sentences. A constructor has the same name as its class, yes. But not every occurrence of that class name is a constructor.
Foo is the class name, but the Foo that appears in that code snippet is not a constructor. The wording of one of your earlier posts suggested that maybe you thought it was.
I thought an official constructor is something like
It looks to me like Greeting is the name of the class and Greeting is the name of the constructor. Are they not the same?
Yes, the name of the class and the name of the constructor are the same.
How do they differ?
As long as you realize that the names are the same but the constructor and the class are two different things, you're on the right track.
A constructor is similar to a method, but it's not a method. It's a block of code that is executed as part of the object creation process to put a newly created object into a valid initial state. If we do not provide any constructors in our code, the compiler supplies one that takes no arguments and does nothing except call the superclass's no-arg constructor.
Mansukhdeep Thind wrote:A constructor, as its name suggests, is a member of the class which has the same name as the class itself.
How does the word "constructor" suggest that a constructor is a member of the class (A constructor is actually not a member.), or that it has the same name as its class?
All this helps me to understand. While this remains a bit challenging to me. I say that humbly, since this is basic stuff, but I desire to understand it. I thought I understood it before, then turns out there were gaps in my understanding.
I will go over this more and trust it will sink into my mind better. I do not want to hold you guys up further today. I do sincerely appreciate all this.
I am determined to understand Java. Some things seem to make more sense than other things.
Post by:autobot
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking