简体   繁体   中英

“The method is not defined for the type” error in simple program in java

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);


    }

}

more should be located outside the constructor.

That's why indentation is extremely important.. Together we'll make the planet a better place to live in - Indent your code :)

You seem to have declared your more method within the Fetch constructor.

This should not compile, nor make the method accessible from outside the class.

To fix this, add one closing curly bracket before the more method declaration.

More必须位于fetch构造函数之外。

You have a missing } after the while loop. There is a clue in that this class would also show up as having errors!

You have implemented the more(int, int) method within your Fetch() constructor . Please verify { and }

current code

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

need to change to like below

class  Fetch {
   public Fetch() {

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

更多应该在构造函数之外,更多的是你错过了紧密的大括号///

Replace your Fetch class with below, you have some issues with braces in class:

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM