File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object fields vs instance variables

 
Andy James
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
childAge is instance variable here and newAge is local variable.I didn't find anything named 'age' in your program.
 
Andy James
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew

Your response has indeed cleared up the misunderstanding here.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andy James
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic