Get reference for current class

10,260

Solution 1

You should use the class literal as follows -

Test.class

Solution 2

static method cannot have this reference, static method is associated with class state.

You can use Test.class.

Share:
10,260
kasi
Author by

kasi

Updated on July 26, 2022

Comments

  • kasi
    kasi almost 2 years

    maybe it is trivial question, but is there some way in Java how to get current class reference? Something like this for class, not for object? Eg. in static method I need to refer to current class, how can I get it?:

    public class Test {
        public static void test(){
            this.getClass(); // not working, can not use this to return class object Test
        }
    }
    

    To more specify my question:

    Is something in JAVA like this for current class, or do I have to use ClassName.class, even if I am inside some class, to get this class reference?