Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does .class mean after a class name

Tags:

java

What does .class mean in, for example, MyClass.class? I understand that when you put the name of a class and the point it is used to access its static fields or methods, but that doesn't relate to '.class'

like image 916
user2055603 Avatar asked Feb 26 '13 23:02

user2055603


People also ask

What does .class mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

Why .class is used in Java?

This '. class' method is used in Java for code Reflection. Generally you can gather meta data for your class such as the full qualified class name, list of constants, list of public fields, etc, etc.

What does class reference mean?

A class reference is a special meta type that can be used to refer to classes (not instances of them) within a certain subtree of the class hierarchy. This allows to write code that can work polymorphically with classes – for example dynamically instantiate different subclasses or call virtual static methods.

What do you mean by object & class?

class: a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods). object: an object is an element (or instance) of a class; objects have the behaviors of their class.


1 Answers

SomeClass.class gets the Class<SomeClass> type which you can use for programming using the reflection API.

You can get the same type if you have an instance of the class using instance.getClass().

You can check out the documentation here. For example, you can:

  • get the names of fields and methods, annotations, etc.
  • invoke methods, get constructor to create new instances of the class
  • get the name of the class, the superclass, package, etc
like image 105
Andrew Mao Avatar answered Oct 18 '22 18:10

Andrew Mao