Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Methods  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java book I'm reading mentions "constructor methods", which sounds like methods that are somehow used to build an object from a class template. Do all classes I create need to have constructor methods defined? What exactly are they? Do the objects created form a class contain the classes constructor methods?
Thanks,
Landon
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guessed right. Constructor methods are used to instantiate an instance of a class. Note that they can have different signatures, however. For example:

In the above, I skipped other constructors you would normally see, such as one for just hours and minutes. Also note that normally you would provide for some sort of consistent state checking of these values, rather than just assigning whatever comes in (through the constructor) to a variable.
One last thing...every constructor call is automatically preceeded with a call to the 'super' constructor...you can't see it, but it's automatically the first line in any constructor. If you think about this, you will realize that the Object constructor is the first one to actually run, as this 'daisy chain' of constructor calls only stops there. Also note that this does NOT happen when objects are finalized(). Some recommend calling the super.finalize() method before coding your own, but it's not required. Java is 'supposed' to handle that for you.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Thanks for your response to my questions. I had a couple of questions about your code example. Is the
public Time();
statement the constructor method for the Time class?
If so why is it listed three times? Are those just different examples of what the constructor method for the Time class could have looked like? If I create a new class, what is included in the default constructor method?
For example:
I have a class called 'point'.
It contains 4 variables.
xCoordinate
yCoordinate
zCoordinate
pointType
It also has a couple of methods that change or display the value of these 4 variables.
What would my constructor method for this class include? Would it be a statement that creates the 4 variables, or a statement that assigns them values?
Thanks so much for your help.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In David's example, the "Time" class has three constructors. Yes, you can overload constructors just as with any other method. For example, with the "java.util.ArrayList" class you can create new ArrayList instances as such:

This implies that there are at least two constructors defined for the "ArrayList" class.
Java has several rules regarding constructors:
1) Constructors aren't inherited. If you extend a parent class, the new child class doesn't automatically get all of its parent's constructors.
2) Every class must have a constructor. If you don't provide at least one constructor, one will be provided for you (a default, no-parameter one).
3) The first thing that every constructor does is to invoke another constructor, either by doing "super(...)" or "this(...)". If you don't supply one, a call to "super()" is automatically inserted.
For your "Point" class, you might want to do something like this:

If the user doesn't supply any parameters, you get a default point at 0,0,0. The second constructor lets them specify the x,y,z coordinate and a point type.
Think of all the logical ways in which people might want to create instances of your Point class, and create constructors for each. But don't go crazy.
In general, it's a good rule to put most/all of the logic in one constructor, and have all the other constructors invoke the *real* one. That way if you want to change your implementation you only have to modify one method.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne,
Thanks for your explanation. I appreciate your help. If you have time, I had a couple more questions.
What is the difference between 'super' and 'this' when referencing a constructor method? Does 'super' pass on the constuctor methods of the superclass to the subclass?
Also, just so I'm clear, a constuctor method sets up an object with values for it's object variables, right?
For instance, in my point object example, you had a constructor method to asign default values to my variables and a constructor method that assigned values to the variables as input by the user. Is this correct?
Thanks for all the help everyone.
Landon
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, "this(...)" invokes another constructor in the same class. "super(...)" invokes a parent constructor. There is no way to jump over the parent and invoke a constructor two or more levels up. You can go to the parent and that's it.
The constructor is a place for you to do any initialization you want, generally based on parameters passed in. And, yes, in my example you can create Points two ways:

With "point2" the system will automatically upcast the int values to double for you.
You can also specify default values with the variable declaration, as with:

There are actually four ways to set your instance variables:
1) Do nothing (Java will provide defaults)
2) Set a value with the declaration
3) Set them in an initializer block: { xCoordinate = 0; }
4) Set them in a constructor
Method #1 points out a key difference between instance variables and local variables (in methods). Java will set every instance variable for you, so you can use them w/out an explicit assignment. You can't do that with local variables. You must set them before referencing them.
Don't worry too much about #3, as it's something that's almost never used for instance variables.
The last method is the only place where user-specified values can be used.
Anyway, I think you have a good grasp of what's going on. Good luck with Java!
[ December 11, 2003: Message edited by: Wayne L Johnson ]
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Wayne....Just one more question.
Does the constructor method that utilizes user input automatically overwrite a constructor method set up with defualt values?
Thanks again for your help.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in Java, constructors are not methods and they are not called constructor methods. (I've heard a rumor that constructors are called constructor methods in some other language(s).)
In Java, methods are members of something - they're either class members, in the case of static methods, or they're instance members, in the case of non-static methods. Also, methods return a value, though that "value" can be void. Since methods are members, they can be inherited.
Constructors are not members of anything. They are not inherited. They cannot return any values.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the constructor method that utilizes user input automatically overwrite a constructor method set up with defualt values?
The term overwrite is used when referring to one instance method overwriting another instance method with the same signature, from a super class. One constructor never overwrites another constructor.
But I don't think that's what you meant to be asking about.
If by "user input" are you intending to be saying "parameters"? If that is the case, then if the user of a constructor invokes a constructor that takes parameters, and supplies those parameters, then the constructor that takes no arguments, if it exists, is not invoked explicitly by the user, but the constructor that the user did invoke might contain a line of code that does invoke the no argument constructor. Remember, the first thing that a constructor must do is to invoke another constructor. The programmer of the constructor that takes arguments could have instructed it to first invoke the no argument constructor with a call to this() - but that would be a strange thing to do.
Is your question answered someplace in there? If not, then perhaps you could be a bit more clear about what you're curious about.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!