aspose file tools*
The moose likes Beginning Java and the fly likes what a constructor is (confirmation) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what a constructor is (confirmation)" Watch "what a constructor is (confirmation)" New topic
Author

what a constructor is (confirmation)

David Starr
Ranch Hand

Joined: Apr 08, 2013
Posts: 54
In the Hello World application, is the constructor the actual name of the class? (i.e. HelloWorld)

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the world of Java David. What is the dictionary meaning of the word "constructor"?


~ Mansukh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
David Starr
Ranch Hand

Joined: Apr 08, 2013
Posts: 54
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

Joined: Jul 27, 2010
Posts: 1157

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?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Apr 08, 2013
Posts: 54
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: what a constructor is (confirmation)