JVM如何处理静态变量? [英] How JVM handles static variable?

查看:326
本文介绍了JVM如何处理静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A{
  static int a=5;
  int b=6;

  void method(){
    //method body
     a++;
  }
}

JVM如何处理静态变量处理静态变量。例如...

How JVM handles static variable handles static variable. for example...

A object1=new A();
A object2=new A();

上面的代码将在两个不同的内存位置创建两个A类对象。此外,还将创建两个变量b 的实例。 变量a 会发生什么。哪个对象将保存静态变量的引用?当我们更新静态变量时会发生什么?

Above code will create two objects of class A in two different memory location. Also two instance of variable b will be created. What will happen for variable a. which object will hold the reference for the static variable? and what happen when we update static variable?

推荐答案


哪个对象将保存参考静态变量

"which object will hold the reference for the static variable"

对象不包含静态变量,因为它们没有绑定到任何对象,它们绑定到类。
应用于类(而不是实例数据)的类和所有数据都存储在堆的永久生成部分中。

Objects don't hold static variables as they are not bound to any object, they are bound to class. Classes and all of the data applying to classes (not instance data) is stored in the Permanent Generation section of the heap.

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

这篇关于JVM如何处理静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆