What is the class "class" in Java?

Java 8Object Oriented ProgrammingProgramming

The Java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor.

Example

Following is the example demonstrates the usage of the class Class.

The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name.

Live Demo

import java.lang.*;

public class ClassDemo {
   public static void main(String[] args) {
      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();
      
      //Returns the canonical name of the underlying class if it exists
      System.out.println("Class = " + cls.getCanonicalName());
   }
}

Ouput

Class = com.tutorialspoint.ClassDemo
raja
Published on 08-Jan-2018 10:24:17
Advertisements