Languages
[Edit]
EN

Java - get class name

10 points
Created by:
Suzannah-Estrada
340

In this short article, we would like to show how to get some class name in Java.

Quick solution:

String className = MyClass.class.getName();

Example output:

path.to.package.MyClass

 

Practical example

In the below example, getName() function returns the full path to the class - package name + class name.

package com.example;

public class Program {

    public static void main(String[] args) {

        System.out.println(MyClass.class.getName());  // com.example.MyClass
    }
}

Output:

com.example.MyClass

 

Example MyClass.java file:

package com.example;

public class MyClass {

    // some code inside ...
}
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join