Java - get class name

10 points
Created by:

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:


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




Example file:

package com.example;

public class MyClass {

    // some code inside ...
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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.

❤️💻 🙂