Forum Stats

  • 3,826,669 Users
  • 2,260,688 Discussions
  • 7,897,046 Comments

Discussions

BicycleDemo non static variable error

1ceb0e66-5cab-47dc-ae57-acc6bc165daf
edited Apr 12, 2017 8:13PM in New To Java

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....

Answers

  • Unknown
    edited Apr 12, 2017 8:13PM
    In Oracle Docs - What is a Class, I copied/pasted Bicycle demo to test in class and got syntax error:

    What 'Oracle Docs' are you referring to?

    Did you get that code from The Java Tutorials here?

    https://docs.oracle.com/javase/tutorial/java/concepts/class.html

    If so then you copied it wrong. The code in that tutorial has TWO classes that should be in TWO DIFFERENT files.

    The 'Bicycle' class should be in a file named 'Bicycle.java'. That class does NOT have a 'main' method. And it is NOT part of the BicycleDemo class as you posted:

    public class BicycleDemo{        class Bicycle     {

    The 'BicycleDemo' class should be in a file name 'BicycleDemo.java'. That class DOES HAVE a 'main' method and DOES NOT include the Bicycle class as the link above shows.

    class BicycleDemo {  public static void main(String[] args) {  // Create two different   // Bicycle objects

    Use the code at the link above. Don't know where you got the code you posted.

This discussion has been closed.