0

What exactly is the meaning of the following construction: MyClassName.class ?

At first I thought MyClassName.class would represent the access of a static class variable defined for MyClassName class, but it that'd be true then the following assignment should be possible:

MyClassName m = new MyClassName();
Class<MyClassName> clazz = m.class; //access static class variable by using an instance variable

What is the true meaning of MyClassName.class? Is it a static class variable? is it a Java special construction?

Daniel
  • 1,861
  • 1
  • 15
  • 23

1 Answers1

1

Every class in Java has an associated instance of java.lang.Class which contains metadata about the class i.e its attributes, types, methods, super class etc.

Here MyClassName.class is called class literal. Link to Java doc for further info - http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.8.2

skool99
  • 780
  • 1
  • 16
  • 35