Hey there,

I am getting used to Java, but here is something that is stumping me:

 // Fig. 3.10: GradeBook.java
 // GradeBook class with a constructor to initialize the course name.

public class GradeBook
 private String courseName; // course name for this GradeBook
// constructor initializes courseName with String argument
public GradeBook( String name ) // constructor name is class name
courseName = name; // initializes courseName
} // end constructor
 // 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

The object then is initialized:

// Fig. 3.11: GradeBookTest.java
 // GradeBook constructor used to specify the course name at the
 // time each GradeBook object is created.

public class GradeBookTest
 // main method begins program execution
 public static void main( String[] args )
 // create GradeBook object
GradeBook gradeBook1 = new GradeBook(
"CS101 Introduction to Java Programming" );
GradeBook gradeBook2 = new GradeBook(
"CS102 Data Structures in Java" );
display initial value of courseName for each GradeBook
 System.out.printf( "gradeBook1 course name is: %s\n",
 gradeBook1.getCourseName() );
 System.out.printf( "gradeBook2 course name is: %s\n",
 gradeBook2.getCourseName() );
 } // end main
 } // end class GradeBookTest

But here is the thing, why do I need a set method, if the constructor is already srtting it. Thanks :)

Recommended Answers

All 6 Replies

Member Avatar

Google constructer versus setter for more info, constructor should be called only once -on creation, whereas setter can be called again and again, changing what should be the private attributes of the class.

So let me check to make sure I am on the right track, the object is initialized, and the name perimeter is accessible by the other method, thus that is how it is initialized, passed to the set method, and passed to the get method? Or is the name string private?

Member Avatar

No you should be able to comment out the set method and your program should function exactly the same I think. I've not used java in a while to test it.

The purpose of having the set method is sometime down the line you might want to change the course name. If you didn't have a set method you would have to create a new object i.e with the constructor.

As you can imagine this has a cost-> memory/resources are used when you iniatialise a new object and it destroys all the other attributes that might already be associated with that object.

As iamthwee pointed out, if you put in the constructor the name argument, that's ok, your object will set the name attribute to whatever you want, but, if along the run of your program, you want to make changes to your object, e.g. the course name has been changed, you must change it too, than you would have to have a method of a sort to access it. Since you commented out the setter, the best way to do this is to create a new object, with the name changed. Creating a new object will cost you resources, and it won't be able to communicate to other elements previous object was communicating (e.g. previous object was in a list etc.).
Since it goes down to the point of either having 1 more line in your class, or create a new object everytime you want a rename, I'll go with the first one.

One case where you won't have a setter is where the field is immutable by design. Often this will be some kind of ID or serial number field which cannot change becuase it's used as the unique persistent identifier fr this object (the primary key in the underlying data storage).

in a bit a "real time" example: a person is born, you set his age to: 0.
let's say, you run a thread. each year on the same day, you have to change that persons age to age+1.
you can't, however, do that using the constructor, because if you do, you'll create a second person.
so, you'll use setter to 're-'set the value of age for that particular person.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.