• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what a constructor is (confirmation)

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Hello World application, is the constructor the actual name of the class? (i.e. HelloWorld)

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the world of Java David. What is the dictionary meaning of the word "constructor"?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic