Java 局部变量、实例字段、输入参数和类字段有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20671008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a local variable, an instance field, an input parameter, and a class field?
提问by Jordan McGowan
What is the difference between a local variable, an instance field, an input parameter, and a class field with respect to a simple Java program?
就一个简单的 Java 程序而言,局部变量、实例字段、输入参数和类字段之间有什么区别?
采纳答案by Makoto
A local variableis defined within the scope of a block. It cannot be used outside of that block.
甲局部变量是一个块的范围内限定。它不能在该块之外使用。
Example:
例子:
if(x > 10) {
String local = "Local value";
}
I cannot use local
outside of that if
block.
我不能local
在那个if
块之外使用。
An instance field, or field, is a variable that's bound to the object itself. I can use it in the object without the need to use accessors, and any method contained within the object may use it.
一个实例字段或字段,是绑定到对象本身的变量。我可以在对象中使用它而不需要使用访问器,并且对象中包含的任何方法都可以使用它。
If I wanted to use it outsideof the object, and it was not public
, I would have to use getters and/or setters.
如果我想在对象之外使用它,而它不是public
,我将不得不使用 getter 和/或 setter。
Example:
例子:
public class Point {
private int xValue; // xValue is a field
public void showX() {
System.out.println("X is: " + xValue);
}
}
An input parameter, or parameteror even argument, is something that we pass into a method or constructor. It has scope with respect to the method or constructor that we pass it into.
一个输入参数,或者参数,甚至争论,是值得我们传递到方法或构造函数。它具有与我们传递给它的方法或构造函数相关的范围。
Example:
例子:
public class Point {
private int xValue;
public Point(int x) {
xValue = x;
}
public void setX(int x) {
xValue = x;
}
}
Both x
parameters are bound to different scopes.
这两个x
参数绑定到不同的范围。
A class field, or static field, is similar to a field, but the difference is that you do not need to have an instance of the containing object to use it.
一类场,或静态字段,类似于一个领域,但不同的是,你不需要有包含对象的实例来使用它。
Example:
例子:
System.out.println(Integer.MAX_VALUE);
I don't need an instance of Integer
to retrieve the globally known maximum value of all ints.
我不需要一个实例Integer
来检索所有整数的全局已知最大值。
回答by Tom Heard
Not quite.
不完全的。
A class field is what you think a local variable is but it is generally a static field and so is the same across all instances.
类字段是您认为的局部变量,但它通常是静态字段,因此在所有实例中都是相同的。
An instance field is the same as a class field, but is non static and can be different for each instance of the object.
实例字段与类字段相同,但不是静态的,并且对于对象的每个实例可以不同。
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
And a local variable is a variable inside a method or block, that can only be used by that method or block.
局部变量是方法或块内的变量,只能由该方法或块使用。
Oh and your input parameter definition is correct, an input parameter is a field that is passed to a method as a parameter.
哦,您的输入参数定义是正确的,输入参数是作为参数传递给方法的字段。
回答by Shafiq Jetha
回答by Peter Lawrey
A local variable is local to a method.
局部变量是方法的局部变量。
An instance fields is the field of an instance of a class i.e. an object.
实例字段是类的实例的字段,即对象。
A parameter is passed to a method
一个参数被传递给一个方法
A class field, I assume is a static field which is associated with the class. e.g. if you use multiple class loaders, you can have multiple classes with the same name and their own static fields.
一个类字段,我假设是一个与类相关联的静态字段。例如,如果您使用多个类加载器,您可以拥有多个具有相同名称和它们自己的静态字段的类。
回答by MadProgrammer
Start by having a read through Classes and Objects
首先通读类和对象
I know the local variable is a variable that is available to the class it is in, correct?
我知道局部变量是一个可供它所在的类使用的变量,对吗?
No, generally a local variable refers to a variable that only has context within the area it was declared. This typically refers to variables declared within methods and {...}
blocks (like if
statements)
不,通常局部变量指的是仅在其声明的区域内具有上下文的变量。这通常是指在方法和{...}
块(如if
语句)中声明的变量
An instance field is an Object that is created in the constructor...?
实例字段是在构造函数中创建的对象...?
Not really, an instance field is any field, declared at the class level which is not static, therefore it's value only has meaning to an individual instance of the class
不是真的,实例字段是在类级别声明的任何字段,它不是静态的,因此它的值仅对类的单个实例有意义
An input parameter is what is passed into a method.
输入参数是传递给方法的内容。
Yes
是的
But I have NO idea about a class field!
但我不知道类字段!
A class field and instance field are (generally) the same thing. The only difference would be if the field is declared static
, then it can't be a instance field...
类字段和实例字段(通常)是同一回事。唯一的区别是如果声明了该字段static
,则它不能是实例字段...
回答by trainrobbery
A local variableis a variable in a method. It's scope is limited to the scope of the two parenthesis around it. {}
甲局部变量是在一个方法的变量。它的范围仅限于它周围的两个括号的范围。{}
Example:
例子:
public void someMethod () {
int localVariable1 = 5;
if (...) {
int localVariable2 = 7;
}
}
With an instance field, I think you mean a member of a a class instance. If you take for example the class Dimension
, this would be height
or width
.
.
An input parameteris a parameter in a method, as you guessed.
对于实例字段,我认为您的意思是类实例的成员。如果以类为例Dimension
,这将是height
or width
。. 一个输入参数是一个方法的参数,因为你猜。
A class fieldis a field in a static method.
甲类字段是在静态方法中的一个字段。