Error saving method in variable

Class c = v.getClass(); try { Method m = c.getMethod("something"); if(!m.getReturnType().equals(Boolean.TYPE)) {return false;} } catch(NoSuchMethodException e) {return false;} 

... where v is an object of a particular class.
When I try to compile this, I get:

error: cannot find character
Method m = c.getMethod ("something");
^

Method is a type that is located in java.lang.reflect.Method . According to my knowledge, java.lang and all subsequent ones are imported by default, but I even did this explicitly:

 import java.lang.*; 

So my question is: how can I get my compiler to recognize the Method class, or how can I save the return value of getMethod otherwise?

PS: Please ignore the unverified call to the getMethod method, this will be a problem at another time (maybe another question).

+5
source share
2 answers

Classes from the java.lang. automatically imported, but this does not apply to nested packages. And this is true not only for java.lang.* , But for all packages in general - nested packages are not imported automatically, and if you need any class from a nested package, you must explicitly import it. Like this:

 import java.lang.reflect.Method; 
+8
source

You need to import java.lang.reflect.Method or java.lang.reflect.* . Importing java.lang.* Does not include the java.lang.reflect , since java.lang.reflect not a subpackage of java.lang (there is no package hierarchy in Java).

+2
source

Source: https://habr.com/ru/post/1239780/


All Articles