ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.