How do I create a static method that returns the name of the class in Java?

Asked 2 years ago, Updated 2 years ago, 111 views

public class MyClass {
    public static String getClassName() {
        String name = ???; // How can I return "MyClass"?
        return name;
    }
}

I want to create a static method that returns the name of the class like this, but I don't know what to do.

static java

2022-09-22 12:19

1 Answers

MyClass.class.getName(); // Returns all class names up to package names.

MyClass.class.getSimpleName(); // Returns only the class name.

You can write whichever you want.


2022-09-22 12:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.