Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does .class mean in Java?

People also ask

Why is .class used in Java?

Yes, a class in Java is simply a template for creating objects with similar attributes and behavior. As a template, the class defines the attributes and behavior that objects constructed from it can exhibit.

What is class <?> Mean in Java?

In object-oriented programming, a class is a basic building block. It can be defined as template that describes the data and behaviour associated with the class instantiation.

What is the difference between .java file and .class file?

java file contains your Java source code while a . class file contains the Java bytecode produced by the Java compiler. It is your . class files that run on the JVM to execute a Java application.

What is class forName in Java with example?

forName(String name, boolean initialize, ClassLoader loader) method returns the Class object associated with the class or interface with the given string name, using the given class loader. The specified class loader is used to load the class or interface.


When you write .class after a class name, it references the class literal - java.lang.Class object that represents information about given class.

For example, if your class is Print, 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());

.class is used when there isn't an instance of the class available.

.getClass() is used when there is an instance of the class available.

object.getClass() returns the class of the given object.

For example:

String string = "hello";
System.out.println(string.getClass().toString());

This will output:

class java.lang.String

This is the class of the string object :)


Just to clarify, this '.class' method is not referring to the bytecode file you see after compiling java code nor a confusion between the concepts of Class vs. Object in OOP theory.

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.

Check these links (already mentioned above) to get all the details:
https://docs.oracle.com/javase/tutorial/reflect/class/classNew.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

Normally you don't plan on using Reflection right away when you start building your project. It's something that you know you need after trying to manage already working code. Many times you need it to manage multiple instances of your program. Maybe you want to identify each particular 'clone' to determine if something is already defined, or count the number of functions, or just simply log the details of a particular instance of your class.


If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass()

The .class Syntax

If the type is available but there is no instance then it is possible to obtain a Class by appending .class to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

boolean b;
Class c = b.getClass();   // compile-time error

Class c = boolean.class;  // correct

See: docs.oracle.com about class