In java, what does such a class compile with?

Below is the code defining the type class

:

package annotationtype;

public class Example {

    public static void main(String[] args){


    }
}

      

which is functionally compiled with javac

:

public class annotationtype.Example{
    public static Class<annotationtype.Example> class;
    {
        class = Class.forName("annotationtype.Example")
    }
    public annotationtype.Example(){}
    public static void main(java.lang.String[] args){}
}

      

My main focus is on the static member variable Class<annotationtype.Example> class

in the above code. Also, this member variable Class<annotationtype.Example> class

actually points to an object of the type class Class

that maintains the metadata class Example

once loaded class Example

into memory.

Do I understand correctly?

+3


source to share


2 answers


Class literals are part of the language specification as stated in JLS 15.8.2

A class literal is an expression consisting of the name of a class, interface, array, or primitive type or void pseudorange, followed by a '.' and the token class.

The C.class type, where C is the name of the class, interface, or array type (ยง4.3), is class <C>.

The type p.class, where p is the name of the primitive type (clause 4.2), is the class <B>, where B is the type of expression of type p after (ยง5.1.7).

The void.class type (ยง8.4.5) is the <Void> class.

javac

does not create a static field class

for each class, but it recognizes the class expression and compiles it correctly.

Take, for example, a class:



public class Hello {
        public static void main(String[] args){
                Class<?> myClass = Hello.class;
                System.out.println("Hello, " + myClass);
        }
}

      

Will compile (only relevant bytecode sections included):

public class Hello   minor version: 0   major version: 52   flags:
 ACC_PUBLIC, ACC_SUPER 
Constant pool:    
#1 = Methodref          #11.#20        // java/lang/Object."<init>":()V    
#2 = Class              #21            // Hello
......
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=3, locals=2, args_size=1
     0: ldc           #2                  // class Hello
     2: astore_1

      

You can see that I javac

put a reference to the Hello class in the constant pool and then loaded that constant when I access it in main

.

+4


source


If by "functionally compiled" you mean that the field is class

"available to you, unless you explicitly declare it, then you are correct. Of course class

not a field, it is a class literal in the java language."

Also, if we compile javac Example.java

then disasemble javap -c Example

we are left with this:

Compiled from "Example.java"
public class Example {
  public Example();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: return
}

      

Please note that there is no reference to the class

. On the other hand, the default constructor Example()

magically looks like this, it's fair to say that



public class Example {
    public static void main(String[] args){
    }
}

      

compiles to

public class Example {
    public Example(){}
    public static void main(String[] args){
    }
}

      

0


source







All Articles