简体   繁体   English

静态方法的Java实例变量

[英]Java instance variable of a static method

when does instance variable of a static method gets memory in java? 静态方法的实例变量何时在Java中获取内存? it is perfectly right to create instance variable in static method (either in parameter or declaring inside the function), but i want to know when does the instance variable of static method is allocated memory, if no object of the class is created? 在静态方法中创建实例变量是完全正确的(在参数中或在函数内部声明),但是我想知道静态方法的实例变量何时分配内存,如果没有创建该类的对象?

Actually i meant to say the local variable inside static method 其实我是说静态方法里面的局部变量

Firstly, the term instance variable is not accurate, I am assuming that you are asking about local variable such as a and b in the example below: 首先, instance variable一词不准确,我假设您在下面的示例中询问诸如ab类的local variable

void method(int a) {
    int b = 3;
}

In Java, only primitives and references are stored on stack, objects are stored in heap when they are constructed. 在Java中,只有原语和引用存储在堆栈中,而对象在构造时存储在堆中。 Stack will be cleaned as soon as the scope ends, heap is cleaned by garbage collector. 范围一结束,堆栈将被清除,垃圾收集器将清除堆。

Here is an example: 这是一个例子:

public class Main  {

    static A a = new A();

    static void method() {
        int b = 2;
        C c = new C();
    }

}

The first time your code refers to Main , class loader will load the class and initialize all its static fields - object new A() will go into heap. 您的代码第一次引用Main ,类加载器将加载该类并初始化其所有静态字段-对象new A()将进入堆。 Nothing more happens here, method method could as well not exist. 这里什么也没有发生,方法method也可能不存在。

When you call method method , it will add value 2 on the stack, then it will add reference c on the stack which will be pointing to the object new C() in the heap. 当您调用method method ,它将在堆栈上添加值2 ,然后在堆栈上添加引用c ,该引用将指向堆中的对象new C()

When method exists, 2 and c are removed from the stack (there is actually no removal, but the top of the stack is changed to two positions below so these two values will be overriden whenever something else comes onto stack), while new C() will remain in heap until garbage collector triggers. 当method存在时,将2c从堆栈中删除(实际上没有删除,但是堆栈的顶部更改为下面的两个位置,因此无论何时有其他东西进入堆栈,这两个值都将被覆盖),而new C()将保留在堆中,直到垃圾回收器触发为止。 It's likely that it will be garbage collected immediately as GC may detect that there are no more references to this object. 由于GC可能检测到不再有对该对象的引用,因此可能会立即对其进行垃圾回收。

If you declare a variable or use parameter inside a static method, its not at all instance variable. 如果在静态方法中声明变量或使用参数,则根本不是实例变量。 Its a local variable and it will be initialized once the method gets invoked 它是一个局部变量,方法被调用后将被初始化

static void methodTest(int i) {

String s = "Hello";

}

Here s is a local variable to that method. s是该方法的局部变量。 and i is the function parameter which is also a local variable to the method i是函数参数,它也是方法的局部变量

There is not such thing as an instance variable in a static method. 静态方法中没有实例变量之类的东西。

If you mean parameters and local variables, those are allocated on stack (any created objects being allocated on heap as usual). 如果您指的是参数和局部变量,则它们将分配在堆栈上(所有创建的对象将照常分配在堆上)。

Static fields of the class will be initialized when the class is loaded. 类的静态字段将在加载类时初始化。

Instance variables of a class will be allocated (and possibly initialized) when an instance is constructed. 构造实例时,将分配(并可能初始化)类的实例变量。

The variable defined by static method is still local variable in scope of the method, just as if non-static method created local variable. 静态方法定义的变量仍然是方法范围内的局部变量,就像非静态方法创建的局部变量一样。 However, problem is that the variable never gets garbage collected, as long as the class loader (usually main class, which gets "unloaded" only at runtime termination, which is like scratching your left ear with your right hand) is not unloaded and garbage collected. 但是,问题在于,只要不卸载类加载器(通常是主类,仅在运行时终止时才“卸载”该类,就像用左手抓挠左耳一样),该变量就永远不会被垃圾回收集。 And objects get GC'd only if they are not referenced anymore, and in case of static fields, they have tendency to be referenced even when nothing uses them because of the behavior I described. 而且,只有在不再引用对象的情况下,对象才会获得GC,并且在静态字段的情况下,即使由于我描述的行为而没有使用它们,它们也倾向于被引用。 So in large applications, where memory and resources are issue, it is pretty bad idea to declare anything inside static methods since you pretty soon pollute the memory with LOCAL VARIABLES that cannot be collected. 因此,在存在内存和资源问题的大型应用程序中,在静态方法中声明任何内容是一个绝妙的主意,因为您很快就会用无法收集的局部变量来污染内存。

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

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