Ron
Ron

Reputation: 19

Error "non-static variable this cannot be referenced from a static context" when instantiating object

I get the error "non-static variable this cannot be referenced from a static context" when trying to instantiate an object from the 'Boat' class. Any help would be appreciated!

package simpleboatapp;

/**
 *
 * @author ragarwala
 */
public class SimpleBoatApp {

    public class Boat {

        private float speed;
        final private float topSpeed = 90;
        private boolean sailPosition;

        public Boat()
        {
            speed = 0;
            sailPosition = false;
        }
        public void goFast (String boatName)
        {

            if (speed < topSpeed)
            {
                speed = speed + 10;
                sailPosition = true;
                System.out.println (boatName + "is raising the sail at speed of" + speed + "mph");
            }
            else
            {
                sailPosition = true; 
                System.out.println (boatName + "is raising the sail at maximum speed of" + speed + "mph");
            }
        }

    }

  /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Boat simpleboat = new Boat(); <--- Error occurs for this line
        simpleboat.goFast("Destiny"); 
    }

}

Upvotes: 1

Views: 154

Answers (4)

intcreator
intcreator

Reputation: 4414

There are a few solutions:

  • Put the Boat class into a different class file. If Boat is going to be a non-static class and you want to instantiate it inside a static class, it should be in its own file.
  • Make the Boat class static. If you make the Boat class static you'll be able to instantiate it inside of the Main function and the program will run.
  • Change how you instantiate theBoat class. You can instantiate it as follows:

    Boat simpleboat = new Main().new Boat();

    This will resolve the static/non-static conflict.

Upvotes: 1

M Sach
M Sach

Reputation: 34424

Its the non static (inner) class which you can initiate like below

   Boat simpleboat = new SimpleBoatApp().new Boat();

If it would have static class, you could initiate like below

   Boat simpleboat = new SimpleBoatApp().Boat();

Upvotes: 0

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31279

You Boat class does not need anything from the containing class BoatApp.

In that case you can declare it is a static member class using the static keyword and you'll be able to instantiate it from a static context.

public static class Boat {

When you declare a class inside another class, the default assumption is that the class inside may access the data in the outer class. Which means that it always need to have an instance of the outer class in scope.

You can search for "inner class" if you want to know more about that.

But in this case, your BoatApp is clearly not something that you want to access from within your member class Boat (it's just a holder for a main method) so you just need to declare Boat as static, or you move it to its own file.

Upvotes: 1

David Yee
David Yee

Reputation: 3646

The problem is that you have the Boat class enclosed within another class, SimpleBoatApp, while trying to statically instantiate a new Boat without the class that encloses it.

Instead, consider moving the Boat class outside the SimpleBoatApp class while leaving your main method inside your SimpleBoatApp:

public class SimpleBoatApp {
    public static void main(String[] args) {
        Boat simpleboat = new Boat();
        simpleboat.goFast("Destiny"); 
    }
}

Upvotes: 0

Related Questions