Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BicycleDemo non static variable error

1ceb0e66-5cab-47dc-ae57-acc6bc165dafApr 12 2017 — edited Apr 12 2017

Hello;

In Oracle Docs - What is a Class, I copied/pasted Bicycle demo to test in class and got syntax error:  "non static variable this cannot be referenced from a static context"  Using BlueJ IDE.  I removed static from main method and would compile, but then it is not running as written in Oracle Docs ????  here's my code:

public class BicycleDemo

{

   

    class Bicycle

    {

          int cadence = 0;

          int speed = 0;

          int gear = 1;

        

         void changeCadence(int newValue) {

         cadence = newValue;

         }

         void changeGear(int newValue) {

         gear = newValue;

         }

         void speedUp(int increment) {

         speed = speed + increment;

         }

         void applyBrakes(int decrement) {

         speed = speed - decrement;

         }

         void printStates() {

         System.out.println("cadence:" +

         cadence + " speed:" +

         speed + " gear:" + gear);

         }

    }

    public static void main(String[] args)

    {

         System.out.println("This is the output of BicycleDemo");

        

         // Create two different Bicycle objects

         Bicycle bike1 = new Bicycle();

         Bicycle bike2 = new Bicycle();

        

         // Invoke methods on those objects

         bike1.changeCadence(50);

         bike1.speedUp(10);

         bike1.changeGear(2);

         bike1.printStates();

         bike2.changeCadence(50);

         bike2.speedUp(10);

         bike2.changeGear(2);

         bike2.changeCadence(40);

         bike2.speedUp(10);

         bike2.changeGear(3);

         bike2.printStates();

    }

}

Any and all suggestions are greatly appreciated....

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 10 2017
Added on Apr 12 2017
1 comment
345 views