开发者

Can the terms 'variable' and 'field' be used interchangeably in java?

开发者 https://www.devze.com 2022-12-20 10:23 出处:网络
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:

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 w开发者_StackOverflow中文版ithin a method, while fields are part of a class.


Edit:

You have the right idea.

After going back to the Java Documentation, I'll use their terminology:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

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.


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.


Yes, that's correct. Fields are also called members.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消