File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's exam: Mutator method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic

Dan's exam: Mutator method

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Which of the following are true statements?
a. A top-level class can not be called "tightly encapsulated" unless it is declared private.
b. Encapsulation enhances the maintainability of the code.
c. A tightly encapsulated class allows fast public access to member fields.
d. A tightly encapsulated class allows access to data only through accessor and mutator methods.
e. Encapsulation usually reduces the size of the code.
f. A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.

What is mutator method? If it refers to manupulation of class data members, then it is not appropriate for data access. Therefore d should be not correct.....
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
"mutation" means "alteration"/change. So, mutator methods means, methods that allows modification of objects. In java layman terms it would be "setter" methods.
Also, you might be aware of the fact that "String" is an immutable object. We can't change string. Every operation results into "new" string object created.
Hope this helps.
Doug Dunn
Ranch Hand

Joined: Aug 03, 2003
Posts: 66
Accessor methods access the state of an object by returning either the value of an instance variable or a value derived from the value of an instance variable (such as a formatted date). They typically are named getWhatever(). Mutator methods change the state of an object by assigning values to instance variables. They are typically named setWhatever(). Be careful, however, the notion of declaring public accessor and mutator methods for all of the instance variables in a class was one of the early misconceptions about how to declare a class in object-oriented prpgramming languages. Classes should implement behaviour and automatically declaring accessor and mutator methods for all of the instance variables in a class effectively breaks encapsulation.
Methods that appear to be mutator methods but in fact return a new instance of an immutable class are what I call the Would-Be Mutator design pattern, which is a proven programming technic for the design of immutable classes. IT SHOULD NOT BE CONFUSED WITH MAKING DEFENSIVE COPIES, however. No less than Joshua Bloch does this in Effective Java. Making defensive copies is a programming technic that is absolutely necessary in order to encapsulate references to mutable objects. These are two very different motivations.

Download a copy of <a href="" target="_blank" rel="nofollow"><i>"Mastering The Fundamentals of The Java Programming Language"</i></a>
I agree. Here's the link:
subject: Dan's exam: Mutator method
It's not a secret anymore!