getting illegal start of expression error

前端 未结 2 1539
傲寒
傲寒 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");
    
    0 讨论(0)
  • 2021-01-16 21:05

    You're trying to define a method (CreateNew) within a method (main), which you cannot do in Java. Move it out of the main; and as model and status appear to be instance variables (not method variables), move them as well:

    public class Robot {
        // Member variables
        String model;
        /*int year;*/
        String status;
    
        // main method
        public static void main(String args[]){
    
            // Presumably more stuff here
        }
    
        // Further method    
        public String CreateNew () {
            Robot optimus;
            optimus = new Robot();
            optimus.model="Autobot"; 
            /*optimus.year="2008";*/
            optimus.status="active";
            return (optimus.model);
        }
    }
    

    Based on its content, you may want CreateNew to be static (so it can be called via Robot.CreateNew rather than via a Robot instance). Like this:

    public class Robot {
        // Member variables
        String model;
        /*int year;*/
        String status;
    
        // main method
        public static void main(String args[]){
    
            // Presumably more stuff here
        }
    
        // Further method    
        public static String CreateNew () {
        //     ^----------------------------- here's the change
            Robot optimus;
            optimus = new Robot();
            optimus.model="Autobot"; 
            /*optimus.year="2008";*/
            optimus.status="active";
            return (optimus.model);
        }
    }
    

    Used as

    String theModel = Robot.CreateNew();
    

    ...although it's unclear to me why you want to create a Robot instance and then throw it away and just return the model instance member's value.


    Somewhat off-topic, but the overwhelming convention in Java is that method names (static or instance) start with a lower-case letter, e.g. createNew rather than CreateNew.

    0 讨论(0)
提交回复
热议问题