Java Constructors
Hey, I think I get what Java constructors are, but let me just make sure about their perimeters: are they for initializing some values for an object? If so, great, I get it, but still a problem:
{
private String courseName; // course name for this GradeBook
public GradeBook( String name )
{
courseName = name; // initiates the course name
}
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
} // end method setCourseName
// method to retrieve the course name
public String getCourseName()
{
return courseName;
} // end method getCourseName
// display a welcome message to the GradeBook user
public void displayMessage()
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
System.out.printf( "Welcome to the grade book for\n%s!\n",
getCourseName() );
} // end method displayMessage
} // end class GradeBook
I get why you would have a set method, or a initialization constructor, but why both? If you are only going to use the object to pass values, why have a set method? Is it required to have a set method, even if the values don't change? I should also mention that I have a constructor, which uses the same perimeters as the methods. This confuses me.
Last edited by zacb on 14 Jun 2013, 6:35 pm, edited 2 times in total.
When instantiating the object it does not take the string as an argument, and so the Set method must be employed to take that string.
Ah, now I remember, that is not a constructor at all, it is a class. A constructor can be set there and can take an argument...
A constructor would be public GradeBook(String strName)
etc, etc...
A constructor should initialize an object into a valid state. Setter methods should be used for setting fields to valid values. A constructor can use the setter methods of the object to create that valid state.
Consider the following problem: you have to make sure that the course name is valid (i.e. it is not null and its length is bigger than 0).
You could do this:
public GradeBook(String name) {
if(name != null && name.length() > 0) {
this.courseName = name;
} else {
// Perhaps throw some exception here
}
}
While the above is perfectly valid, your constructor can become quite messy when you have more parameters. Plus you might have to repeat those checks in your setter method if the field can be edited by other objects. To keep things clear and your code DRY you can move that validation code to the setter method.
public void setCourseName(String name) {
if(name != null && name.length() > 0) {
this.courseName = name;
} else {
// Perhaps throw some exception here
}
}
And then change your constructor to:
public GradeBook(String name) {
this.setCourseName(name);
}
If the course name is not allowed to be edited after it has been set it's still a good idea to create a setter method, though you could make it private so that other objects can not access the method.
The difference is that a constructor is a method that is used by the JVM's "class loader" when the class is instantiated. and this only happens once. You are not required to have either a Set <attribute> method nor a Get <attribute> method.
So:
String theCourse = "Advanced Java";
Gradebook myBook = new GradeBook(theCourse);
versus:
String theCourse = "Advanced Java";
GradeBook myBook = new GradeBook();
.... other stuff happens....
myBook.setCourse( theCourse);
Sometimes you wind up doing it the second way because the value of "theCourse" is not available at the time you create the class. ALSO: You should always have a No Argument constructor regardless of whatever else you; it prevents "accidental" use of the class in ways you did not intend. If there isn't an explicit No Arg. constructor the class loader will just infer one. I can't recall the exact details at the moment, but it prevents some annoying and obscure problems.
Hope this helps.
I usually don't create a setter when my intention is that once the value is passed in via the constructor, I don't want it to be changed.
I don't think this is correct. AFAIK a default constructor is only automagically generated if no other constructors are defined (see http://docs.oracle.com/javase/tutorial/ ... ctors.html). Calling a no argument constructor on a class that doesn't have one (but does have other constructors) will result in errors during compilation.
A constructor can take pretty much any argument. If you're going to change the value of those arguments after the object has been created, you should implement a set method as well—otherwise, you'd have to create a new object with every new value.
Say you have a pair of dice. Everytime you throw the dice without a set method, you'd have to create a new dice object. With a set method, you could instead set the values on the first object.
Constructors are there just to initiaøize the start values and create the objects within the object needed at startup.
In this case you'd probably want both the constructor to take the name argument, and also have a set method for the name argument. The constructor, as someone mentioned above, should initialize the object into a valid state. If for some reason this object would be considered valid without the name property having been set, then you could have a zero-argument constructor.
As for the set method - you'd want to have this as well, unless there is some reason that you'd want the course name to be set when the object is first created, and then you want it to be impossible to change the name later on.