Forums Register Login

what a constructor is (confirmation)

+Pie Number of slices to send: Send
In the Hello World application, is the constructor the actual name of the class? (i.e. HelloWorld)

+Pie Number of slices to send: Send
Welcome to the world of Java David. What is the dictionary meaning of the word "constructor"?
+Pie Number of slices to send: Send
No. In your code there is no explicit constructor. The name of the class is just the name of the class.

In your case, a default constructor will be provided since none was specified.

If you do specify a constructor, it will be named the same as the class. But that doesn't make the class name itself a constructor.
+Pie Number of slices to send: Send
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.

See Providing Constructors for Your Classes in Oracle's Java Tutorials.

The example class you posted doesn't have a constructor.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:

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, or that it has the same name as its class?



Take it back.
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1145 times.
Similar Threads
Is There No Such Thing as a Root Package?
how many objects created
doubt about inheriting static methods
Quest About Constructors??
abstract
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:07:15.