Q
Q
quad692022-02-27 04:51:51
Java
quad69, 2022-02-27 04:51:51

How to get current class in static method?

There is a class, let's say it's some Test1, and it has a static find method, which should return an array of instances of this class. The question is how to get the current class in a static method, because the same getClass for example does not work?

A specific indication of the name of the class is not suitable, because other classes will inherit from this class and this will not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-27
@quad69

No way. Think of a static method as a function that has nothing to do with the class (except that the static method is declared within it). It's just that there are no global functions in Java, everything is done within classes. And this leaves only static methods for global functions. You can transfer the static method to another class - and everything will work (when you fix the links, of course). Also a static method can't have anything to do with inheritance.
I'm guessing what you're after is called a "factory" pattern and is usually done on a singleton basis rather than a static method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question