Java program to represent the class in different ways

Java example to represent the class in different ways.
Submitted by Nidhi, on April 29, 2022

Problem Solution:

In this program, we will represent classes in different ways and print class names.

Program/Source Code:

The source code to represent the class in different ways is given below. The given program is compiled and executed successfully.

// Java program to represent the class in 
// different ways

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls1 = Class.forName("java.lang.String");
    Class cls2 = String.class;
    Class cls3 = int.class;

    System.out.println("Class represented by cls1: " + cls1);
    System.out.println("Class represented by cls2: " + cls2);
    System.out.println("Class represented by cls3: " + cls3);
  }
}

Output:

Class represented by cls1: class java.lang.String
Class represented by cls2: class java.lang.String
Class represented by cls3: int

Explanation:

In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. Here, we used Class.forName() method and .class property to represent classes. Then we printed the name of classes.

Java Class and Object Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.