简体   繁体   English

为什么在类中定义的变量不是局部变量而是java中的实例变量?

[英]why variable defined in a class is not a local variable but instance variable in java?

According to the doc Local variables in java are declared in methods, constructors, or blocks. 根据doc,java中的局部变量在方法,构造函数或块中声明。

In the below Class A isn't xa local variable too since it is in blocks({}) i know they are called as instance variable but i am confused? 在下面的A类不是xa局部变量,因为它是在块({})我知道它们被称为实例变量,但我很困惑? If yes Access modifiers cannot be used for local variables but i am sure i can add public private protected ? 如果是访问修饰符不能用于本地变量,但我相信我可以添加公共私有保护? It also says that local variable are stored in stack but as per the below code x will be stored in heap right since they are part of the instance? 它还说局部变量存储在堆栈中但是根据下面的代码,x将被存储在堆中,因为它们是实例的一部分?

class A{

private int x = 5; // Isn't this a local varibale too since it is in blocks 

}

.

class A{

public void function(){
int x = 5; // this is a local variable since it is declared in a function
private int x2=5; // Error Access modifiers cannot be used for local variables
}

}

In the below Class A isn't xa local variable too since it is in blocks({}) 在下面的类A中也不是xa局部变量,因为它在块({})中

No. It's not in a block. 不,它不在一个街区。 It's in a class declaration, but that's not a block as such. 它在一个类声明中,但这不是一个 "Block" isn't synonymous with "text in braces". “Block”与“大括号中的文字”不是同义词。

To be a bit clearer, local variables are declared in: 为了更清楚一点,局部变量在:

If you look at the production for a class declaration , that's not a Block (unlike the production for static initializers and instance initializers). 如果你看一下类声明的产生 ,那就不是一个Block(与静态初始化器和实例初始化器的生产不同)。

In the below Class A isn't xa local variable too since it is in blocks({}) 在下面的类A中也不是xa局部变量,因为它在块({})中

Your x is not a local variable, it's an instance variable. 你的x不是局部变量,它是一个实例变量。 Block means, instance initialization block or static block or try catch block. 块意味着,实例初始化块或静态块或尝试捕获块。

Access modifiers cannot be used for local variables but i am sure i can add public private protected ? 访问修饰符不能用于局部变量,但我确定我可以添加公共私有保护?

It doesn't make sense to have access modifiers to method local variables, since, method local variable can only be accessed inside the method. 对方法局部变量使用访问修饰符没有意义,因为方法局部变量只能在方法内部访问。

I'm not entirely sure where you are reading. 我不完全确定你在哪里读书。 Local variables are values declared inside a method. 局部变量是在方法内声明的值。

There is no special keyword designating a variable as local; 没有特殊的关键字将变量指定为本地变量; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. 该决定完全来自声明变量的位置 - 它位于方法的开始和结束括号之间。

Taken from here . 取自这里

A local variable is a variable that has a local scope and is generally not expected to last a long time. 局部变量是具有局部范围的变量,通常不会持续很长时间。

If you declare a variable inside a method, that variable will only be relevant while that method is being executed, and after the method finishes the variable will be discarded. 如果在方法中声明变量,则该变量仅在执行该方法时才相关,并且在该方法完成后,该变量将被丢弃。

An instance variable is a variable that is bound to an instance of "something" and is generally considered to last as long as that "something" lasts. 实例变量是绑定到“某事”实例的变量,并且通常被认为只要“某事”持续就会持续。

If you declare a variable inside a class, then when you create a new instance of the class the variable will also be created and pinned onto the class. 如果在类中声明变量,那么当您创建类的新实例时,该变量也将被创建并固定到类上。 The variable will be there as long as the instance object of the class that you created exists, if you decide to discard the instance of the class you made, then the instance variable gets discarded as well. 只要您创建的类的实例对象存在,变量就会存在,如果您决定放弃所创建的类的实例,那么实例变量也会被丢弃。

Hope this was intuitive. 希望这很直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM