getting illegal start of expression error

前端 未结 2 1544
傲寒
傲寒 2021-01-16 20:37

i\'m totally new to java. i \'m try to create my first program & i get this error.

E:\\java>javac Robot.java
Robot.java:16: error: illegal start of ex         


        
2条回答
  •  失恋的感觉
    2021-01-16 21:04

    You didn't close your main method before you create the CreateNew() one. In fact I don't think you meant to have a main method in your Robot class, you should have only one main method for your whole program. And your CreateNew should be a constructor:

    public class Robot {
            String model;
            /*int year;*/
            String status;
    
            public Robot () {
                this.model="Autobot"; 
                this.status="active";
            }
        }
    }
    

    and then in another class that contains your main method (or it could be in the same class too):

    public class OtherClass {
        public static void main(String[] args) {
            Robot optimus = new Robot(); // here you create an instance of your robot.
        }
    }
    

    then you can have a second constructor that takes in parameter the model and status like that:

    public Robot (String m, Status s) {
            this.model=m; 
            this.status=s;
    }
    

    and finally in your main:

    Robot prime = new Robot("aName", "aStatus");
    

提交回复
热议问题