title: Java reflection: How to get methods with no parameters only
body: <p>I'm working on a school assignment about Java reflection. The details are below:</p>
  
  <blockquote>
    <p>Write a console program that asks the user for a class name, loads
    that class and creates an instance of it. We assume that the class has
    a constructor without any parameters. Then, the program prints out the
    names and values of the public variables of the created object, and
    also a list of the public methods that do not specify a parameter. The
    program should let the user choose a method and execute that method on
    the created object. Afterwards, the program should again show the
    public variables with their values and allow the user to choose a
    method, and so on. Use the following class to test your
    implementation:</p>
  
  <pre><code>public class Counter {
      public int c;
      public void increment() { c++; }
      public void decrement() { c--; }
      public void reset() { c = 0; }
  }
  </code></pre>
  </blockquote>
  
  <p>The problem I am having has to do with the following sentence: "list of the public methods that do not specify a parameter". Is there a way to list only methods with no parameters? I have used getMethods but I end up getting a lot of methods from the Object and Class superclasses with parameters.</p>
  
  <p>For example the following code that I have written:</p>
  
  <pre><code>import java.lang.reflect.*;
  import java.io.*;
  
  public class Q1 {
      public static void main(String[] args) {
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
              System.out.print("What class would you like to run? ");
              String className = reader.readLine();
  
              Class c = Class.forName(className);
              Object o = c.newInstance();
  
              for (Field f : c.getFields())
                  System.out.println(f);
              for (Method m : c.getMethods())
                  System.out.println(m);
  
          } catch(IOException e) {
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          } catch (InstantiationException e) {
              e.printStackTrace();
          } catch (IllegalAccessException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Outputs the following:</p>
  
  <blockquote>
    <p>What class would you like to run? Counter<br>
    public int Counter.c<br>
    public void Counter.reset()<br>
    public void Counter.increment()<br>
    public void Counter.decrement()<br>
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException<br>
    public final void java.lang.Object.wait() throws java.lang.InterruptedException<br>
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException<br>
    public boolean java.lang.Object.equals(java.lang.Object)<br>
    public java.lang.String java.lang.Object.toString()<br>
    public native int java.lang.Object.hashCode()<br>
    public final native java.lang.Class java.lang.Object.getClass()<br>
    public final native void java.lang.Object.notify()<br>
    public final native void java.lang.Object.notifyAll()</p>
  </blockquote>
  
  <p>Is there a way to get only the ones with no parameters to be printed? Also is my interpretation of the assignment details right in the first place? Or does the phrase "public methods that do not specify a parameter" possibly mean something else and I have entirely the wrong idea?</p>