aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Beans naming convention for getter and setter methods 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 "Java Beans naming convention for getter and setter methods" Watch "Java Beans naming convention for getter and setter methods" New topic
Author

Java Beans naming convention for getter and setter methods

Asanka Kodippili
Greenhorn

Joined: Dec 09, 2008
Posts: 21
I'm a little confused over the JavaBeans naming convention for getter and setter methods. according to Kathy sierra's book

"getSize()is a valid JavaBeans getter name for a property named
"size."
Keep in mind that you do not need to have a variable named size

(although some IDEs expect it). The name of the property is inferred from the
getters and setters, not through any variables in your class. What you return
from getSize() is up to you."
-Chapter 1: Declarations and Access Control, page 9

is this equally true for setter methods?

or should we definitely use the property name as part of the method name?

and if the property name and getter and setter method names don't match, do we have to explicitly declare somewhere as to which getter and setter belongs to which property?

i know we wouldn't probably get this kind of question in the exam, but just in case, what is the correct answer for the question below:

class Sajith {
Stirng private name;
private int age;
}
what are valid setters and getters for the above class ?

a.getName
b.setName
c.getSmName
d.setXyz
e.getAge
f.setAge

according to the quotation above, should i also select c and d as correct answers ?


SCJP6 (81%)
Favorite quote from K&B with regard to exam prep : "What matters most is not WHAT you know, its WHEN you know it "
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Asanka, welcome to javaranch.

First of all your name is not according to the naming policy of javaranch so please change it.

As far as your question goes, the book is right that you are not bound to have a field for every accessor (AKA getter) and mutator (AKA setter) to have a corresponding field. But that is a matter of concern when you make your own programs. For the exam, in the options that you gave, A,B,E and F are correct. Also in the exam you will be told how many options are correct so you will not be caught in conditions like what you have mentioned here. You can safely chose the options that you think are correct without the shadow of a doubt ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3220
    
  49

Hi Asanka,

Hope you are from Sri Lanka. However please change the display name, as Henry and Ankit described above.

Well, making accessor methods (getters/setters) with matching to the name of the related variable name is recommended; but NOT required. It is same as to both getters and setters.

However, if you have a getter method like getField, the name of the setter method must be setField (unless the field is read-only). More specifically, the getter method for a boolan field should be isField() instead of getField().


Cheers.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Originally posted by Devaka Cooray:
the getter method for a boolan field should be isField() instead of getField().


As far as I know, the getter method for a boolean CAN be getField() and isField()...
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Yes, it's correct what Ankit had said. It's clearly mentioned in the SCJP 6 K&B book. The programmer is not bound to make method name as isField() in case field is a boolean variable.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Valentine Wu
Greenhorn

Joined: Jan 31, 2008
Posts: 16
All the choices are correct.

You can have

public String getSmName()
{
return "Sr. " + name;
}

and
public void setXyz(String name)
{
this.name = name;
}


SCJP 6.0 (93%)
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3220
    
  49

Originally posted by Ankit Garg:

As far as I know, the getter method for a boolean CAN be getField() and isField()...


Originally posted by Rajshekhar Paul:

The programmer is not bound to make method name as isField() in case field is a boolean variable.


Well, using isField() for boolean type is RECOMMENDED, but NOT REQUIRED. Especially, it is recommended for EJB persistence entity classes.
Asanka Kodippili
Greenhorn

Joined: Dec 09, 2008
Posts: 21
Thank you all for the replies :-)Its more clear to me now.
i'm sorry about the previous display name, but since my actual surname comes before my first name and happens to be very long , i chose the initials . Hope the new 1 is ok.
Today is a sad day for me since i think i will have to postpone my scjp exam. i fixed the date for 18th Dec and have to notify before 48 hours if i need to change the date. and that's today .
I've tried the master exam n failed narrowly. (53%) and tried the diagnostic exam and 1st exam from Devaka's examlab and got 33% and 30%. I finished all three exams in 2 hrs and had another hour free , but the 72 questions exhausted me so much that i just clicked end without checking my answers again. ( ok, i know i'm too lazy to b alive)
Now planning to take the exam on 1st Jan. Would b grateful for any tips. (I'm an undergrad with my experience being limited to uni projects, none of which were in java) I've gone through the KS study guide twice and tried some questions from other sources. Please Help !!
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
Hi Asanka,
you got a good score in Master Exam.As per my experience Actual exam is much more easier than this.I scored around 65% and got 94% in actual exam.I didn't tried Devaka Cooray's exam.But I heard that it is harder than others.Your exam date is on 1st.If you utilize this time you can not only pass the exam ,can get a nice score.What I can suggest you that you take some rest before and after any test, so that you can fully concentrate on your exam and can judge you properly.
Best of luck for your exam.


Thanks,<br />Subhasish
Asanka Kodippili
Greenhorn

Joined: Dec 09, 2008
Posts: 21
Thanks Subashish :-D i went and fixed my new exam date today (2nd Jan) and after reading many posts on javaranch about 1.5 and 1.6 versions of the exam, i decided to change the version to 1.6. So i'll be using the next 2 weeks to prepare for that :-) Thanks to all of you again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Beans naming convention for getter and setter methods