0

I have in my code this if

if (method.getName().equals("ignore")) {

it's pointing to to a static method.

Can I somehow call getName on MyClass::ignore ??

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Pan Bydlak
  • 567
  • 6
  • 13
  • What type is `method` in the above? Or is it hypothetical? – T.J. Crowder Sep 29 '14 at 15:42
  • possible duplicate of [invoking a static method using reflection](http://stackoverflow.com/questions/2467544/invoking-a-static-method-using-reflection) – Michael Petch Sep 29 '14 at 15:47
  • 1
    I sense a possible XY Problem type question here. There's a short answer, and a longer more correct answer that will depend on the background information that we don't know yet til you tell us. In other words, there's likely a much better way to do what you're trying to do than to try to get a method name this way. Please tell us the details of your problem, not how you're trying to solve it, so that we can give accurate help. – Hovercraft Full Of Eels Sep 29 '14 at 15:49
  • @HovercraftFullOfEels method is of type java.lang.reflect.Method. I try not to use hardcoded string value, but method reference. The method is pointing to is checking for is IgnoreLogging.ignore. – Pan Bydlak Sep 30 '14 at 07:41
  • @MichaelPetch I ask abotu new Java8 features! – Pan Bydlak Sep 30 '14 at 07:41
  • @MarounMaroun method is of type java.lang.reflect.Method. I try not to use hardcoded string value, but method reference. The method is pointing to is checking for is IgnoreLogging.ignore. – Pan Bydlak Sep 30 '14 at 07:42
  • @T.J.Crowder method is of type java.lang.reflect.Method. I try not to use hardcoded string value, but method reference. The method is pointing to is checking for is IgnoreLogging.ignore. – Pan Bydlak Sep 30 '14 at 07:49

1 Answers1

2

No, there is no static (compile time) way to reference a method as a Method object. You'll have to dynamically retrieve it through Class#getMethod(..) (or other appropriate methods). Or use a String value for its name as you are currently doing.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724