"Non-static variable this cannot be referenced from a static context" when creating an object

mko :

I wrote the below code to test the concept of classes and objects in Java.

public class ShowBike {
    private class Bicycle {
        public int gear = 0;
        public Bicycle(int v) {
            gear = v;
        }
    }

    public static void main() {
        Bicycle bike = new Bicycle(5);
        System.out.println(bike.gear);
    }
}

Why does this give me the below error in the compiling process?

ShowBike.java:12: non-static variable this cannot be referenced from a static context
        Bicycle bike = new Bicycle(5);
                       ^
Alvin Wong :

Make ShowBike.Bicycle static.

public class ShowBike {

    private static class Bicycle {
        public int gear = 0;
        public Bicycle(int v) {
            gear = v;
        }

    }

    public static void main() {
        Bicycle bike = new Bicycle(5);
        System.out.println(bike.gear);
    }
}

In Java there are two types of nested classes: "Static nested class" and "Inner class". Without the static keyword it is an inner class and you will need an instance of ShowBike to access ShowBike.Bicycle:

ShowBike showBike = new ShowBike();
Bicycle bike = showBike.new Bicycle(5);

Static nested classes and normal (non-nested) classes are almost the same in functionality, it's just different ways to group things. However, when using static nested classes, you cannot put definitions of them in separated files, which will lead to a single file containing a lot of class definitions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

non-static variable this cannot be referenced from a static context issue when instantiating a new object

From Dev

Why I can not make object of abstract class here and I got error of Non-static variable this cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static variable cannot be referenced from a static context java

From Dev

Non-static variable cannot be referenced from a static context?

From Java

Why do I get "non-static variable this cannot be referenced from a static context"?

From Dev

Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

From Java

java : non-static variable cannot be referenced from a static context Error

From Dev

Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

From Dev

Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

From Dev

Java JTextField and non-static variable cannot be referenced from a static context

From Dev

non-static variable this cannot be referenced from a static context Dormitory dormitory = new Dormitory();

From Dev

Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

From Dev

DocTest fails when creating an object

From Java

Error when creating an object TriangulatedMesh

From Dev

IndexOutOfBoundsException when creating an object (Java)

From Dev

TypeError when creating a date object

From Java

Declaring a method when creating an object

From Dev

MissingMethodException when creating an object with reflection

From Dev

Returning PromiseValue when creating an object

From Dev

In R, use an if statement for an object when creating a list

From Dev

'MultiOutputClassifier' object is not iterable when creating a Pipeline (Python)

From Dev

Nullpointer when creating a minimal json-object

From Java

Using Lock Object when creating a singleton

From Dev

object is not callable error when creating a form in django

Related Related

  1. 1

    non-static variable this cannot be referenced from a static context issue when instantiating a new object

  2. 2

    Why I can not make object of abstract class here and I got error of Non-static variable this cannot be referenced from a static context

  3. 3

    non static variable this cannot be referenced from a static context

  4. 4

    "non-static variable this cannot be referenced from a static context"?

  5. 5

    java: non-static variable this cannot be referenced from a static context

  6. 6

    Non static variable cannot be referenced from static context java

  7. 7

    non-static variable cannot be referenced from static context [JAVA]

  8. 8

    non-static variable cannot be referenced from a static context java

  9. 9

    Non-static variable cannot be referenced from a static context?

  10. 10

    Why do I get "non-static variable this cannot be referenced from a static context"?

  11. 11

    Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

  12. 12

    java : non-static variable cannot be referenced from a static context Error

  13. 13

    Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

  14. 14

    Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

  15. 15

    Java JTextField and non-static variable cannot be referenced from a static context

  16. 16

    non-static variable this cannot be referenced from a static context Dormitory dormitory = new Dormitory();

  17. 17

    Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

  18. 18

    DocTest fails when creating an object

  19. 19

    Error when creating an object TriangulatedMesh

  20. 20

    IndexOutOfBoundsException when creating an object (Java)

  21. 21

    TypeError when creating a date object

  22. 22

    Declaring a method when creating an object

  23. 23

    MissingMethodException when creating an object with reflection

  24. 24

    Returning PromiseValue when creating an object

  25. 25

    In R, use an if statement for an object when creating a list

  26. 26

    'MultiOutputClassifier' object is not iterable when creating a Pipeline (Python)

  27. 27

    Nullpointer when creating a minimal json-object

  28. 28

    Using Lock Object when creating a singleton

  29. 29

    object is not callable error when creating a form in django

HotTag

Archive