Q:

Java program to represent the class in different ways

belongs to collection: Java Class and Object Programs

0

Java program to represent the class in different ways

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to call static block using the anonym... >>
<< Java program to get the class name using the objec...