What does .class mean in Java?

constructor in java
types of classes in java
inheritance in java
classes and methods in java
classes and objects in java with realtime examples
java class method
how to create object in java
java class syntax

What does .class mean in Java? For example, if I created a class called Print. What does Print.class return?


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());

Classes and Objects in Java, Classes and Objects are basic concepts of Object Oriented Programming which If a class does not explicitly declare any, the Java compiler automatically  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. Core properties include the data types and methods that may be used by the object.


.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 :)

Java.lang.Class class in Java, Java provides a class with name Class in java.lang package. Instances of the class an anonymous class. A anonymous class is a like a local classes except that they do not have a name. class definition of anonymous class. }; // returns the  Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.


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

What is a Class?, 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  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.


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.

What Is a Class? (The Java™ Tutorials > Learning the Java , The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later  Java allows a class to be defined within another class. These are called Nested Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top level classes, Inner classes can be Static. Non-static nested classes are also known as Inner classes.


If there is no instance available then .class syntax is used to get the corresponding Class object for a class otherwise you can use getClass() method to get Class object. Since, there is no instance of primitive data type, we have to use .class syntax for primitive data types.

    package test;

    public class Test {
       public static void main(String[] args)
       {
          //there is no instance available for class Test, so use Test.class
          System.out.println("Test.class.getName() ::: " + Test.class.getName());

          // Now create an instance of class Test use getClass()
          Test testObj = new Test();
          System.out.println("testObj.getClass().getName() ::: " + testObj.getClass().getName());

          //For primitive type
          System.out.println("boolean.class.getName() ::: " + boolean.class.getName());
          System.out.println("int.class.getName() ::: " + int.class.getName());
          System.out.println("char.class.getName() ::: " + char.class.getName());
          System.out.println("long.class.getName() ::: " + long.class.getName());
       }
    }

Defining a Class, The first bold line in the following listing begins a class definition block. basic building block of an object-oriented language such as Java--is a template that  Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows −


Java - Object and Classes, If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class. Each time a new object is created, at least one  answered Nov 25, 2019 by Ayush (46.1k points) 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.


Java Classes and Objects, A Class is like an object constructor, or a "blueprint" for creating objects. Create a Class. To create a class, use the keyword class : MyClass.java. Create a class  The main class can have any name, although typically it will just be called "Main". What Does the Main Method Do? The main() method is the key to making a Java program executable.


What is Class and Object in Java OOPS?, CLASS are a blueprint or a set of instructions to build a specific type of object. It is a basic concept of Object-Oriented Programming which revolve  In Java, a static member is a member of a class that isn’t associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance. The two types of static members are static fields and static methods: …