1
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?

  • What do you mean? Memory *isn't* allocated for methods, except when Java loads the class file they're in. – user253751 Mar 25 '15 at 04:29
  • Voting to reopen, since this doesn't look like a duplicate - that question is about overriding static methods, and this question isn't (although I'm not sure what it *is* about...) – user253751 Mar 25 '15 at 04:31
  • http://stackoverflow.com/questions/1932399/is-it-a-bad-idea-to-declare-a-final-static-method?lq=1 – Ruchira Gayan Ranaweera Mar 25 '15 at 04:32
  • I just wanted to know where in JVM memory is allocated to static member functions if they are final, and how they are accessed – Navneet Srivastava Mar 25 '15 at 04:34
  • Do you mean memory for arguments and local variables? – Dawood ibn Kareem Mar 25 '15 at 04:35
  • The problem with your question is that it has nothing to do with your title. To answer the title question, refer to this post: http://stackoverflow.com/questions/1743715/behaviour-of-final-static-method?lq=1. – ktm5124 Mar 25 '15 at 04:50
  • methods whether it is static or instance share the same memory. only instance variables are allocated different memory for different object of that class. – Braj Mar 25 '15 at 05:01
  • The answer is [HERE](http://stackoverflow.com/questions/8387989/where-is-a-static-method-and-a-static-variable-stored-in-java-in-heap-or-in-sta) There are two types of memory 1. Heap 2. Stack. All objects are stored in Heap and local variables and methods calls are stored in Stack. `final` doesn't changes the memory storage. – Braj Mar 25 '15 at 05:03
  • `static` variables are associated with class itself hence stored in the same location where class specific data is stored in heap along with its methods. – Braj Mar 25 '15 at 05:06

2 Answers2

0

When you make a static method final, its hidden in the inheriting class. You can find a decent explanation at http://www.coderanch.com/how-to/java/OverridingVsHiding

Ved Agarwal
  • 565
  • 3
  • 6
  • 14
0

When you create a method with the modifier "final" you are modifying that method so that it cannot be overridden or hidden.

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. 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