Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object fields vs instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object fields vs instance variables" Watch "Object fields vs instance variables" New topic
Author

Object fields vs instance variables

Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Are the object fields the same as instance variables.

If a class defines methods 'getAge' and 'setAge' to read and write value of a variable 'childAge', then what is an object field here - 'childAge', or 'age' (derived from the method names)?

cheers
Andy
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Andy James wrote:Are the object fields the same as instance variables.

If a class defines methods 'getAge' and 'setAge' to read and write value of a variable 'childAge', then what is an object field here - 'childAge', or 'age' (derived from the method names)?

cheers
Andy


None can answer your question until you don't post code,post actual problem.


Tell the difficulties that i am difficult.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Are the object fields the same as instance variables


If by object, you mean the instance of a class then yes, object fields are instance variables.

I prefer the terms class members (for statics) and instance members.
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Oops! I though I was quite clear by specifying the names of the getter/ setter methods and the name of the insance variable.

Here's the code:



Now, I repeat my question? Which of the above is termed as an object field - is it childAge or age.

The variable childAge is the name of the actual variable that is used to store the value. On the other hand, 'age' is calculated from the names of the getter methods - getAge and setAge. I believe I read somewhere that the name of Java properties are calculated from their getter and setter methods because the rest of the world should not be concerned about the name of the exact variable that is used to store a value.

Cheers
Andy
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

childAge is instance variable here and newAge is local variable.I didn't find anything named 'age' in your program.
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Saloni,

I don't think you have much experience with working with Java!

As I mentioned in my earlier post,

'age' is calculated from the names of the getter methods - getAge and setAge.


A class property 'age' in Java will define its getter and setter methods with the names - getAge and setAge:

setAge = word 'set' + property name (Age), with first letter capitalized
getAge = word 'get' + property name (Age), with first letter capitalized

Andy
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

A class property 'age' in Java will define its getter and setter methods with the names - getAge and setAge


That is the standard convention but it isn't enforced in Java. Other developers prefer to remove setters completely and only allow instance variables to be set on construction.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Andy James wrote:

Now, I repeat my question? Which of the above is termed as an object field - is it childAge or age.


Cheers
Andy


Look what you asked, hope other knowledgeable rancher will find 'age' and tell you that it is an object or an instance variable or method or property which class will define it automatically .
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

childAge is instance variable here and newAge is local variable.I didn't find anything named 'age' in your program.


Totally agree with this.
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
I am not here to offend anyone, or to get into war of words, please! I tried to point out what I did to clarify my question further.

May I please request experienced ranchers to answer this simple question.

Cheers
Andy
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Andy

Sorry to be repetitive but the correct answer has already been given to you:

childAge is instance variable here and newAge is local variable.I didn't find anything named 'age' in your program.


Fields names in Java are not derived so just because you have methods like setAge and getAge, doesn't mean there is some hidden derived field called age.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

I've got to back Andy up here somewhat.

In pure Java terms, what's been said is correct. There's an instance variable, or field, called childAge (which ought to be private, so what it's called doesn't matter outside the class) and methods setAge and getAge. Java doesn't support properties directly in the way that a language like VB or C# does.

But, in a context where the Java bean conventions are used - e.g. in a JSP or JSF page, or in a GUI builder - then those conventions would state that getter/setters getAge and setAge correspond to a property age*. In a JSP page, you'd be able to refer directly to child.age. And since this is the OCPJP forum, it's worth pointing out that knowledge of the Java bean conventions is on the syllabus.

*Yes, James is right that in some cases the setter will be omitted. But if a setter existed then it would be called setAge. Without it, you've still got a read-only Javabean property called age.


James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Matthew

saloni answered the question that was put here. Perhaps it could have phrased better so that it was in the context of JSPs, not pure Java.

I also feel saying:

I don't think you have much experience with working with Java!


is not a very constructive response to someone who has answered the question (as it was phrased) correctly.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

James Boswell wrote:saloni answered the question that was put here. Perhaps it could have phrased better so that it was in the context of JSPs, not pure Java.

I was just adding some additional context, since it was clear there was some misunderstanding going on. And, as I said, it's wider than just JSPs: the Javabean conventions are part of the OCPJP exam.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Matthew

Your response has indeed cleared up the misunderstanding here.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

James Boswell wrote:Matthew

saloni answered the question that was put here. Perhaps it could have phrased better so that it was in the context of JSPs, not pure Java.

I also feel saying:

I don't think you have much experience with working with Java!


is not a very constructive response to someone who has answered the question (as it was phrased) correctly.


Thanks James
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Andy James wrote:Are the object fields the same as instance variables.

If a class defines methods 'getAge' and 'setAge' to read and write value of a variable 'childAge', then what is an object field here - 'childAge', or 'age' (derived from the method names)?


Purely as a side response, as I am not entirely sure where this topic is going -- if I got into the a conversation with someone, I would have no issues with "object fields" and "instance variables" being interchangeable. It is highly unlikely that I would get confused during the discussion regarding this. It would also be unlikely to be confused because the variable and its setter/getter didn't follow the JavaBean standard.

Not sure if I answer the question, but one opinion point regardless.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Andy James
Ranch Hand

Joined: Aug 22, 2005
Posts: 90
Thanks for all the responses. I appreciate them all.

I agree that in conversation, we would refer to instance variables as object fields. However, we need to be very specific with the exam objectives specified by Oracle for any of its cert exam. This confusion has arised due to the following exam objective from OCA Java SE 7 Programmer1 certification:

2.3 Read or write to object fields


Given the following code:



What will be the answer to the following certification (mock) exam question?

Question) Select the correct options?
a) An object of class Child can access a field 'age'.
b) An object of class Child can access a field 'newAge'.
c) An object of class Child can access a field 'childAge'
d) An object of class Child can access fields - 'childAge', 'newAge'

cheers
Andy
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Andy James wrote:Thanks for all the responses. I appreciate them all.

I agree that in conversation, we would refer to instance variables as object fields. However, we need to be very specific with the exam objectives specified by Oracle for any of its cert exam. This confusion has arised due to the following exam objective from OCA Java SE 7 Programmer1 certification:

2.3 Read or write to object fields


Given the following code:



What will be the answer to the following certification (mock) exam question?

Question) Select the correct options?
a) An object of class Child can access a field 'age'.
b) An object of class Child can access a field 'newAge'.
c) An object of class Child can access a field 'childAge'
d) An object of class Child can access fields - 'childAge', 'newAge'

cheers
Andy


ans.c) An object of class Child can access a field 'childAge'

and post SCJA/OCJA reltaed post in SCJA/OCJA forum.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Andy James wrote:Thanks for all the responses. I appreciate them all.

I agree that in conversation, we would refer to instance variables as object fields. However, we need to be very specific with the exam objectives specified by Oracle for any of its cert exam. This confusion has arised due to the following exam objective from OCA Java SE 7 Programmer1 certification:



Oh, I see what you are trying to say... I don't have an answer for it, but I guess I can offer another opinion.

Keep in mind that you are not trying to pass the bar exam. And that you don't need to nail everything down tightly. So, yes, depending on interpretation, it may be possible to interpret more than one answer as correct. In that regard, I think the best option is to just pick the interpretation that is more correct.


Andy James wrote:
2.3 Read or write to object fields


Given the following code:



What will be the answer to the following certification (mock) exam question?

Question) Select the correct options?
a) An object of class Child can access a field 'age'.
b) An object of class Child can access a field 'newAge'.
c) An object of class Child can access a field 'childAge'
d) An object of class Child can access fields - 'childAge', 'newAge'


Yes, you can argue that based on the rules of JavaBeans, you have a age property -- and for a class with an actual public age field, it too, has an age property. So, arguably choice A is correct.... but is it the most correct? In my opinion, I agree with Saloni, choice C is arguably more correct.

Henry
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Given that they're using the word "field" rather than "property", I'd agree with the others - I'm pretty sure that C would be the intended answer.
 
 
subject: Object fields vs instance variables
 
Similar Threads
Objects
interface question
Memory accocation in case of arrays
string construction
Serialization is not for static variables - Really true?