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

Difference between a Variable and a Field?

 
Div Raj
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I read in a book that, Instance variables are Non-static fields.
And Class variables are Static fields.
Also Parameters are classified as variables and not fields.

Could any one tell me what's the difference between a Variable and a Field?



Thanks
---------
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A field is valid (in scope) throughout the whole class/object. A field is in fact a kind of variable, but it applies to the whole class/object.
 
Div Raj
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThankYou Ritchie

But I've one doubt; the Class variable/Instance variable is also having their scope throughout the class/object. Right?


Thanks
--------
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class variable is what you call a static field. That has scope throughout the whole class, and is accessible from any method of that class, static or instance (non-static).
An instance field is not accessible from any static parts of the class.
 
Div Raj
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.
So could you please tell me what is a Field?

Thanks
-----------
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any variable declared outside methods, initialisers, etc. If you declare a variable inside the class and not inside a method, initialiser, etc, then it is called a fieldThe variable temperature is declared inside the class and outside the methods; you call it a field. It is accessible and in scope in the two methods shown.
The "variable" degrees is "declared" inside the () for the becomeHotter() method; it is a parameter, and is not a field. Its scope is that method.
The variable heat is declared inside the becomeHotter method. It is a local variable and its scope is from "int heat . . ." to the end of the method. It is not a field.

Does that help?
 
Div Raj
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. Now I got it.

ThankYou Ritchie
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic