While studying java with several books, none of them seem to make it clear when to use which term. Right now I understand it as follows:
Variables are defined within a method, while fields are part of a class.
While studying java with several books, none of them seem to make it clear when to use which term. Right now I understand it as follows:
Variables are defined within a method, while fields are part of a class.
You are correct. Variables can be be local to a method. Fields are variables that belong to the class.
EDIT : Fields can be private
, protected
, or public
.
Edit:
You have the right idea.
After going back to the Java Documentation, I'll use their terminology:
Variables refer to fields, local variables, and parameters.
"Variables" is a more general term than "fields". But your summation is basically correct. A field is a class-level variable.
I think you are right to stress the difference. A variable is something that can change, a field is rather a member that has a value, this value can be final in which case calling it a variable seems a bit strange.
In java, a variable is anything which can change its value over the period of execution, while a field (which can also be called a "member" variable of a class) belongs to a class.
A constant/final can be though of (although some may disagree) as opposite of variable.
A Field belongs to a class and can be a variable or constant/final.