Java中的局部变量和实例变量有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088299/
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 local and instance variables in Java
提问by GuruKulki
Except the scope and the storage differences, are there any other major difference between instance and local variables in java?
除了作用域和存储的不同之外,java 中的实例变量和局部变量还有其他主要区别吗?
采纳答案by Tom Jefferys
One extra thing I can think of:
我能想到的一件事是:
Instance variables are given default values, ie null if it's an object reference, 0 if it's an int.
实例变量被赋予默认值,即如果它是一个对象引用,则为 null,如果它是一个 int,则为 0。
Local variables don't get default values, and therefore need to be explicitly initialized (and the compiler usually complains if you fail to do this).
局部变量没有默认值,因此需要显式初始化(如果你不这样做,编译器通常会抱怨)。
回答by danben
No, you pretty much covered it. An instance variable belongs to an instance of a class, a local variable belongs to a stack frame.
不,你几乎涵盖了它。实例变量属于类的实例,局部变量属于堆栈帧。
Instance variables are initialized to default values, but it's generally good practice to use explicit initializations anyway.
实例变量被初始化为默认值,但无论如何使用显式初始化通常是一个好习惯。
回答by Alex Ntousias
The main differences that I see are in their :
我看到的主要区别在于:
Scope:Local variables are visible only in the method or block they are declared whereas instance variables can been seen by all methods in the class.
作用域:局部变量仅在声明它们的方法或块中可见,而实例变量可以被类中的所有方法看到。
Place where they are declared:Local variables are declared inside a method or a block whereas instance variables inside a class but outside a method.
声明位置:局部变量在方法或块内声明,而实例变量在类内但在方法外。
Existence time:Local variables are created when a method is called and destroyed when the method exits whereas instance variables are created using new and destroyed by the garbage collector when there are no reference to them.
存在时间:局部变量在方法被调用时创建并在方法退出时销毁,而实例变量使用 new 创建并在没有引用时由垃圾收集器销毁。
Access:You can't access local variables whereas instance variables can be accessed if they are declared as public.
访问:您不能访问局部变量,而如果将实例变量声明为公共变量,则可以访问它们。
Where they are declared:Local variables are declared in a method or a block before they are called, whereas instance variables can be declared anywhere in the class level (even after their use).
声明位置:局部变量在调用之前在方法或块中声明,而实例变量可以在类级别的任何位置声明(即使在使用之后)。
EDIT:
编辑:
And I forgot to mention that instance variables always have value, even if it's not assigned by the code (then they will have for example null, 0, 0.0, false). For local variables there must be an assigned value by the code, otherwise the compiler generates an error.
我忘了提到实例变量总是有值的,即使它不是由代码分配的(然后它们将具有例如 null、0、0.0、false)。对于局部变量,代码必须有赋值,否则编译器会产生错误。
回答by giri
main difference is instance variables get default values like int value get zero char gets null but not the local variables. You can leave uninitialized instance variable but where as local variables must be initialized otherwise you will get compiler error.
主要区别在于实例变量获取默认值,如 int 值获取零 char 获取 null 但不是局部变量。您可以保留未初始化的实例变量,但必须初始化局部变量,否则您将收到编译器错误。
回答by Steve Emmerson
One other difference, you don't have to worry about concurrent access to local variables; whereas you do with instance variables in a multi-threaded environment.
另一个区别是,您不必担心对局部变量的并发访问;而您在多线程环境中使用实例变量。
回答by x4u
Aside from all that is already mentioned here, I would like to point out that local variables are a bit faster to access for the JVM. The JVM has got more work to do to read or write a instance variable compared to a a local variable. This is still true for the current Hotspot server JVM because it's not a VM optimization problem, it's rather caused by the fact that a instance variable is visible outside of the method and could thus be accessed from other threads while the method is executed.
除了这里已经提到的所有内容之外,我想指出局部变量对于 JVM 的访问速度要快一些。与局部变量相比,JVM 需要做更多的工作来读取或写入实例变量。对于当前的 Hotspot 服务器 JVM 来说仍然如此,因为这不是 VM 优化问题,而是由于实例变量在方法之外可见,因此可以在执行方法时从其他线程访问这一事实。
回答by user4025466
Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.
在方法、构造函数或块中定义的变量称为局部变量。变量将在方法中声明和初始化,并在方法完成后销毁。
Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.
实例变量是类内但在任何方法之外的变量。这些变量在类加载时被实例化。
回答by Sanchit
Local variable:
局部变量:
- is declared inside a method/constructor or within a block (enclosed in braces)
- must be initialized before use, otherwise it won't compile.
- 在方法/构造函数或块中声明(括在大括号中)
- 使用前必须初始化,否则编译不通过。
Instance variable:
实例变量:
- is declared inside a class.
- initialization is not compulsory: if omitted, it contains default value (0, 0.0,
false
,null
, etc.)
- 在类中声明。
- 初始化不是强制性的:如果省略,它包含默认值(0, 0.0
false
,null
, 等)
回答by Mants
Aside from all the above mentioned differences, I would like to point out one more difference that, instance variablescan have access modifiers(like private, public, protected etc.), but local variableswill not have any access modifiers.
除了上面提到的所有差异,我想指出另一个差异,实例变量可以有访问修饰符(如私有、公共、受保护等),但局部变量不会有任何访问修饰符。
回答by Mants
Local variables are declared within a method.
局部变量在方法中声明。
Instance variables are declared inside a class but not within a method.
实例变量在类内声明,但不在方法内声明。
Local variables do not get a default value. The compiler complains if you try to use a local variable before before the variable is initialised.
局部变量没有默认值。如果在变量初始化之前尝试使用局部变量,编译器会抱怨。
However, instance variables always get a default value. If you don't explicitly assign a value to an instance variable, the instance variable still has a value.
但是,实例变量总是有一个默认值。如果您没有显式地为实例变量赋值,实例变量仍然具有值。
integers 0
整数 0
floating points 0
浮点数 0
Boolean false
布尔假
references null
引用 null