Hello, I'm trying to get a private method:

private static int getValue(String input) { char[] value = input.toCharArray(); if(value[0] == '0') return Integer.valueOf(String.valueOf(value[1])); return Integer.valueOf(input); } 

like this:

  Method testGetValue = Seconds.class.getMethod("getValue", String.class); testGetValue.setAccessible(true); int i = (Integer)testGetValue.invoke(s, "00"); 

on this line

 Method testGetValue = Seconds.class.getMethod("getValue", String.class); 

throws a NoSuchMethodException exception. How to fix?

    2 answers 2

    getMethod() returns only public methods.

    To get a private one, try getDeclaredMethod()

    • Thank you, did not see that you left a comment - Igor Gorbunov

    I apologize, but the problem is solved - not getMethod() , but getDeclareMethod()