0

Constant pool of a class has symbolic reference to fields and methods. Does the constant pool of a class contain symbolic references for methods defined only in the class or references of methods defined in its super class as well.

cloud
  • 1
  • 1
  • 3
    did you go through this post? - http://stackoverflow.com/questions/10209952/what-is-the-purpose-of-the-java-constant-pool – JavaHopper Aug 05 '16 at 03:33
  • try "javap -verbose " to see the constant pool of any class – jamey graham Aug 05 '16 at 03:39
  • @javahopper I did go through that post I am just starting with java so gathered as much as I can. From the post and links I got the idea that the constant pool of a class will only have symbolic references to the methods defined in that class and not inherited methods. Just want a confirmation if my understanding is correct. – cloud Aug 05 '16 at 13:45

1 Answers1

0

You are mixing up references and declarations. The constant pool only contains references to methods and fields, so the question whether the constant pool contains references to method declared in the super class can’t have a definitive answer.

A class’ constant pool might contain reference to super class method, if they are referred in some way, e.g. if the class invokes super class methods. Similarly, A class’ constant pool might contain references to its own declared methods, if they are used internally. But if a class doesn’t invoke one of its own methods and doesn’t reference it otherwise, it won’t have a reference to it in the constant pool.

In contrast, a class file contains a list of its declared methods, including information like their implementation code or annotations, whose entries share information with the constant pool, but it is not contained in the constant pool.

To be more specific, a method reference in the constant pool consists of two references to other pool entries, one specifying the declaring class, the other being a “name&type” entry, again consisting of two references to other pool entries, specifying the method’s name and type signature. In contrast, a declared method, obviously not needing to specify its declaring class, has two direct references to the pool, specifying the name and the type signature.

So when a class declares a method void foo(), there will be two constant pool entries for the name foo and the type signature ()V, which the declaration refers to. If the class also has a reference to that method, which is not mandatory, there will also be a “name&type” entry pointing to the two entries described above and a method reference entry referring to said “name&type” entry and the class entry specifying this class as the declaring class.


To summarize, the constant pool might contain references to methods of arbitrary classes, including those declared in the own class hierarchy, but doesn’t have to. The list of declared methods, which is not to be confused with the constant pool, will only contain methods declared in this class, including those which override super class methods, but no inherited methods.

Holger
  • 243,335
  • 30
  • 362
  • 661