简体   繁体   中英

The method main cannot be declared static; static methods can only be declared in a static or top level type

In my coding, parent class Shape is an abstract object and it has several child classes. My coding is as follow:

import java.util.Random;

abstract class Shape {
    protected Color color;
    protected Point point;

    public Shape(Color color, Point point) {
        this.color = color;
        this.point = point;
    }
    public abstract String Type();
}

class Rectangle extends Shape {
    public Rectangle(Color color, Point point) {
        super(color, point);
    }

    public String Type() {
        return "Rectangle";
    }
    class Triangle extends Shape {
        public Triangle(Color color, Point point) {
            super(color, point);
        }
        public String Type() {
            return "Triangle";
        }
    }
    class Eclipse extends Shape {
        public Eclipse(Color color, Point point) {
            super(color, point);
        }
        public String Type() {
            return "Eclipse";
        }
    }
    public class ShapeTest {
        public static void main(String[] args) {
            Color color = new Color(50, 100, 150);
            Point point = new Point(50, 50);
            Shape[] theShape = {
                new Rectangle(color, point),
                    new Triangle(color, point),
                    new Eclipse(color, point)
            };
            Shape shapechoice;
            Random select = new Random();
            for (int i = 0; i < 10; i++) {
                shapechoice = theShape[select.nextInt(theShape.length)];
                System.out.println("The " + (i + 1) + "type you chose is: " + shapechoice.Type());;
            }
        }
    }

Eclipse says "The method main cannot be declared static; static methods can only be declared in a static or top level type" at public static void main(String[] args){ , but i thought this syntax should be a commonly used format which is kinda fixed? Why at here i need to remove "static"? Sorry I'm new at java and might be blur for this.

Move your main() method from the inner class ShapeTest . You can't put it in a non-static non-top-level class. In your existing code, you could put main() in Rectangle , or you could move ShapeTest to be a top-level class.

You have a class declaration like so:

class Rectangle extends Shape {

}

This is a top-level class.

Next you have an inner class declaration:

class Rectangle extends Shape {
    public class ShapeTest {
    }
}

This is an inner class because it is not static . It is within the instance context of Rectangle and has an implicit reference to Rectangle.this .

Next you have a static method declaration:

class Rectangle extends Shape {
    public class ShapeTest {
        public static void main(String[] args){
        }
    }
}

This static declaration is within an instance declaration of ShapeTest . This is not allowed.

So either declare ShapeTest as a static nested class (weird and confusing to have main in a static nested class):

class Rectangle extends Shape {
    public static class ShapeTest {
        public static void main(String[] args){
        }
    }
}

Or declare ShapeTest in its own ShapeTest.java file as a top level class (much better):

public class ShapeTest {
    public static void main(String[] args){
    }
}

You have to move your main() method to a top level class or static class. Either move the main() method to the class Rectangle (as suggested by Elliott) or make your class ShapeTest static , eg

public class Rectangle {
    public static class ShapeTest {
        public static void main(String[] args) {
        }
    }
}

Then you can call the class with java Rectangle.ShapeTest .

You need to put the static main method in (A) a top-level or (B) static class. You have put the main method in a regular nested class, which is dependent on the instance of the class it is defined in.

Try adding the keyword static to the definitions of Triangle , Eclipse and ShapeTest , like so: public static class ShapeTest .

BTW 1: you would be better off creating multiple files. BTW 2: why are Triangle , Eclipse and ShapeTest inner classes of Rectangle?

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