Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get reference for current class [duplicate]

Tags:

java

oop

class

this

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?

like image 544
kasi Avatar asked May 07 '14 21:05

kasi


People also ask

How do you reference a self class in Python?

The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason why we use self is that Python does not use the '@' syntax to refer to instance attributes.


1 Answers

You should use the class literal as follows -

Test.class
like image 166
Bhesh Gurung Avatar answered Sep 22 '22 12:09

Bhesh Gurung