简体   繁体   English

最终静态方法和变量在Java中的存储方式和位置?

[英]How and where are final static methods and variables stored in java?

class X
{

final static void show()
{

  System.out.println("Show");

}
}
class Y extends X
{

  public static void main(String s[])
  {
     new Y().show();
  }
}

Where and how is memory allocated to final static methods, and how are they accessed? 内存在哪里以及如何分配给最终的静态方法,以及如何访问它们?

When you make a static method final, its hidden in the inheriting class. 将静态方法定型为final时,其隐藏在继承类中。 You can find a decent explanation at http://www.coderanch.com/how-to/java/OverridingVsHiding 您可以在http://www.coderanch.com/how-to/java/OverridingVsHiding中找到不错的解释

When you create a method with the modifier "final" you are modifying that method so that it cannot be overridden or hidden. 当使用修饰符“ final”创建方法时,您正在修改该方法,以使其不能被覆盖或隐藏。

So if you were for some reason to create another class that were to extend your class X (which is known as inheritance), you would not be allowed to modify, or override your show() method. 因此,如果出于某种原因要创建另一个扩展类X的类(称为继承),则将不允许您修改或覆盖show()方法。 Furthermore, any other class would be able to access that method. 此外,任何其他类都可以访问该方法。

If you want a more in depth explanation, I suggest reading the Java API: https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html 如果您需要更深入的说明,建议阅读Java API: https : //docs.oracle.com/javase/specs/jls/se7/html/jls-8.html

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

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