PSY Studios
PSY Studios

Reputation: 1

Syntax Error in instance method when compiled in Java

I have written a piece of code that declares three instance variables and two instance methods. However, when I run the program I receive a syntax error in the checkTemperature method. I have checked my syntax for any missing/over used braces, semi-colons etc. but the code looks fine. I am not sure why I am receiving this error can someone help with this problem? Here is my code below.

public class volcanoRobots {

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub


        /*
        * declared instance variables status, speed,temperature
        */
        String status;
        int speed;
        float temperature;

        /*
        * create first instance method checkTemperature
        */
        void checkTemperature(){
            if (temperature > 660){
                status = "returning home";
                speed = 5;
            }
        }

        /*
        * create second instance method showAttributes
        */
        void showAttributes(){
            System.out.println("Status: " + status);
            System.out.println("Speed: " + speed);
            System.out.println("Temperature: " + temperature);
        }

    }

}

Upvotes: 0

Views: 80

Answers (1)

Vivin Paliath
Vivin Paliath

Reputation: 95598

You can't have a method inside another method. Move that method into the main body of the class and your code should compile. Also, your instance variables aren't actually instance variables; they are variables that are local to the main method. Move those into the main body of the class as well, with proper visibility-modifiers; I suggest making them private.

A few other things:

  • Add visibility modifiers to your methods as well. Again, in this case I would suggest private. Without any modifiers, they are package-private, which is probably not what you want.
  • Follow Java naming conventions; your class should be named VolcanoRobots. Class names are in PascalCase, whereas variable and method names are in camelCase.

So your class will eventually look like this:

public class VolcanoRobots {

    /*
    * declared instance variables status, speed,temperature
    */
    private String status;
    private int speed;
    private float temperature;

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    /*
    * create first instance method checkTemperature
    */
    private void checkTemperature(){
        if (temperature > 660){
            status = "returning home";
            speed = 5;
        }
    }

    /*
    * create second instance method showAttributes
    */
    private void showAttributes(){
        System.out.println("Status: " + status);
        System.out.println("Speed: " + speed);
        System.out.println("Temperature: " + temperature);
    }    
}

Upvotes: 2

Related Questions