• Post Reply Bookmark Topic Watch Topic
  • New Topic

Field, Static field, Instance field what's the difference?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to understand what exactly fields are in Java. So from the conclusion I've come to is that fields are like the instance variables in the beginning of the program the things you declare right after the clas. So all those are instance fields? but if that's right then what exactly are class fields and then just field in general. An example of this would be great!


Thank you!
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static fields are fields that belong to a type. Instance fields are variables that belong to an instance of a type (an object). Local variables are variables that belong to a method.

When we say "fields", we can refer to either instance fields or static fields. Usually which of the two is meant is clear from the context.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:Trying to understand what exactly fields are in Java.

FYI, I think the official term is "variable"; however it seems to be used oddly in some cases, including the (to me) oxymoric phrase "constant variable".

So personally, I like to think of a "field" as either a variable or a constant - ie, a member that that is NOT a method or a nested type definition (class, interface or enum).

HIH

Winston
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, "field" is a database term. I think of Java "fields" as variables or properties.

And I'm with you on the "constant variable" thing. That term has annoyed me since I first heard it. It sounds like Orwellian double-speak.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:And I'm with you on the "constant variable" thing. That term has annoyed me since I first heard it.

Glad I'm not alone.

And I just noticed - we have two Robbins in this thread.

Must be Spring.

Winston
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a grandson called Robin; I had better not show him this thread
The JLS calls them fields. And constant variables Yes, it is oxymoronic. You will have to strangle that and de‑oxygenise the phrase.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!