& ldquo; The method is not defined for type & rdquo; error in a simple program in java

advertisements

So I'm just trying to learn Java and after watching some tutorials and reading some basic stuff I am stuck about why this won't run:

package Test;

public class TestProg {
    public static void main(String[] args) {
        Fetch fetc = new Fetch();
        fetc.more(10, 20);
    }
}

This is the Fetch class code:

package Test;

public class Fetch {

    public Fetch() {

        System.out.println("Fetched!"); 

        int a = 1;
        int b = 2;
        int c;

        while (a < 100 && b < 200) {
            a++;
            b++;
            c = a + b;
            System.out.println(c);
        }

        public void more(int d, int e) {
            System.out.println(d + e);
        }
    }
}

I am getting a "The method more(int, int) is not defined for the type Fetch" error in TestProg. If I remove the code about the "more" method (in both Classes), the rest of the code runs normally. I am stumped as to why this would happen because the code is extremely similar to the example I'm studying.


Your method more(int d, int e) is within the Fetch() constructor

Your fetch method should be as follows (note the marked } closing the constructor):

package Test;

public class Fetch {

    public Fetch() {

        System.out.println("Fetched!"); 

        int a = 1;
        int b = 2;
        int c;

        while (a < 100 && b < 200) {

            a++;
            b++;
            c = a + b;
            System.out.println(c);
        }
    }//<---- NOTE: closing constructor

    public void more(int d, int e) {

        System.out.println(d + e);

    }

}