简体   繁体   English

java中静态变量和方法在哪里加载?

[英]Where does static variables and methods are loaded in java?

I am little bit confused that where did static variables and methods are loaded.我有点困惑静态变量和方法在哪里加载。 we say that static variables and methods are loaded in the static memory.我们说静态变量和方法加载在静态内存中。 bt public static void main() is loaded into stack .Since main() method is also static then how it is possible that main is loaded into stack. bt public static void main() 被加载到堆栈中。由于 main() 方法也是静态的,那么 main 是如何加载到堆栈中的。 and alse is static methods and variable are stored in different positions because we say that methods are loaded in different place in memory.而且还是静态方法,变量存储在不同的位置,因为我们说方法在内存中的不同位置加载。

The stack is where things go when they are invoked/executed.堆栈是调用/执行时发生的事情。 It doesn't matter if it is static or not.它是否是静态的并不重要。 Any running function goes onto the stack where it's local variables and all are held until the stack frame is popped.任何正在运行的函数都会进入堆栈,在那里它是局部变量,并且所有函数都被保留,直到堆栈帧被弹出。

For example, I can have main() call main() recursively over and over.例如,我可以让main()一遍又一遍地递归调用main() Each one would be a new stack frame.每个都是一个新的堆栈帧。 The fact that it is a static function does not change that.它是一个静态函数的事实并没有改变这一点。

Static variables, on the other hand, are different.另一方面,静态变量则不同。 There will only be one instance of them and you know it explicitly.它们只会有一个实例,您明确知道。 So, they can go into special storage and be treated differently (as are other global things like the class definitions and all that).因此,它们可以进入特殊存储并被区别对待(就像类定义等其他全局事物一样)。

The actual implementation of this is not liable to be very useful, nor easily understandable.这个的实际实现不是很有用,也不容易理解。 However, a model of it might help you understand the use of these things.但是,它的模型可能会帮助您了解这些东西的用途。

First of all, data and code are quite different animals in Java.首先,Java 中的数据和代码是完全不同的动物。 Variables are going to have values that change at runtime;变量将具有在运行时更改的值; code never does that.代码从不这样做。 So when you instantiate a class, you are never going to get another copy of the code.所以当你实例化一个类时,你永远不会得到代码的另一个副本。

Consider the class Class - instances of it exist, one per fully-qualified class in the program.考虑类Class - 它的实例存在,程序中的每个完全限定类都有一个。 I think of all code for one class, static or not, as being associated with its Class instance -- 'loaded' with it, if you prefer.我认为一个类的所有代码,无论是否静态,都与它的Class实例相关联——如果你愿意,可以用它“加载”它。 Incidentally, and coincidentally, that's also where I think of its static variables being 'loaded'.顺便说一句,巧合的是,这也是我认为它的静态变量被“加载”的地方。

But the instance variables need multiple copies -- whenever you instantiate the class, you need another copy of them.但是实例变量需要多个副本——每当您实例化类时,您都需要它们的另一个副本。 So they are associated (or loaded) with the instance of the class when instantiated -- think of the pointer to the class as a pointer to a structure that contains all the instance variables of that class, plus a pointer to jump tables to its methods, etc.因此,它们在实例化时与类的实例相关联(或加载)——将指向类的指针视为指向包含该类所有实例变量的结构的指针,以及指向其方法的跳转表的指针, 等等。

I do not know what you mean by public static void main being "loaded onto the stack".我不知道你所说的public static void main被“加载到堆栈上”是什么意思。 Do you mean the code?你是说代码吗? Code never goes onto a stack per se.代码本身永远不会进入堆栈。 It wouldn't make any sense to have code from a (normal) class put on the stack, lost when the current method returns, and then have to load it again if the method were called.将来自(普通)类的代码放在堆栈上,在当前方法返回时丢失,然后在调用该方法时必须再次加载它是没有任何意义的。

I think there's part of your question I'm not getting to because I don't understand what you're asking.我认为你的问题的一部分我没有回答,因为我不明白你在问什么。

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

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