2

I saw code like this:

com.test.model.User.class

I defined User class in com.test.model package

But i don't know .class means..?

I think It isn't a static argument and also I didn't define.

Tell me what .class means and which in class where defined .class i can find .class?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
ITshocked
  • 25
  • 5
  • 1
    possible duplicate of [What does .class mean in Java?](http://stackoverflow.com/questions/15078935/what-does-class-mean-in-java) – sinclair Aug 05 '15 at 17:15

2 Answers2

2

Xyz.class is a syntax provided by Java for obtaining a Class<Xyz> metadata object representing the class corresponding to class Xyz. In your case, it's the User class.

Class<T> objects let you examine the fields and methods of the corresponding class, and in some cases allow you to create instances of the corresponding class. This is part of Java's reflection capabilities.

Class<User> cl = User.class;
for (Method m : cl.getMethods()) {
    System.out.println(m.getName());
}
for (Field f : cl.getFields()) {
    System.out.println(f.getName());
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

User.class

User.class will return the representation of the User class as a Class object.

The formal term for this expression is the class literal, as referenced in Section 15.8.2 of the Java Language Specification.

When you write .class after a class name, it references the Class object that represents the given class.

For example, if your class is Print (it is recommended that class name begin with an uppercase letter), then Print.class is an object that represents the class Print on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of Print.

Print myPrint = new Print();
System.out.println(Print.class.getName());
System.out.println(myPrint.getClass().getName());

Source : What does .class mean in Java?

Community
  • 1
  • 1
Pratik Shah
  • 1,782
  • 1
  • 15
  • 33