Java:Non-Static Variable Cannot Be Referenced from a Static Context Error

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

You can't access a non-static variable from a static context.
Your variables(non-static) are outside of main method(static).

Declare your variables as below:

static ArrayList<Spelare> spelare = new ArrayList<>();
static BlackJackKortlek leken = new BlackJackKortlek(4);
static Dealer dealer = new Dealer();

Or declare it inside main:

public static void main(String[] args) {
ArrayList<Spelare> spelare = new ArrayList<>(); //List with players
BlackJackKortlek leken = new BlackJackKortlek(4); //BlackJackDeck
Dealer dealer = new Dealer();
}

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

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.

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

One approach

Declare Man class as static and you'll be able to access it from within main() which is static as well (not tied to any instance of class Solution):

public static class Man

Another approach

We can also leave class Man non-static and create an instance-level factory-method which will create instances of Man:

public class Solution {

public static void main(String[] args) {
//create two object of every class here
Solution solution = new Solution();
Man m1 = solution.createMan("a1", "b1", 11);
Man m2 = solution.createMan( "a2", "b2", 12);

//output them to screen here
System.out.println(m1.name + " " + m1.age + " " + m1.address);
System.out.println(m2.name + " " + m2.age + " " + m2.address);
}

Man createMan(String name, String address, int age) {
return new Man(name, address, age);
}

//add your classes here
public class Man {
private String name;
private String address;
private int age;

private Man(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
}
}

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

Your nested class (which isn't a subclass, by the way) isn't marked as being static, therefore it's an inner class which requires an instance of the encoding class (JavaApp1) in order to construct it.

Options:

  • Make the nested class static
  • Make it not an inner class (i.e. not within JavaApp1 at all)
  • Create an instance of JavaApp1 as the "enclosing instance":

    GenTest x = new JavaApp1().new GenTest();

Personally I'd go with the second approach - nested classes in Java have a few oddities around them, so I'd use top-level classes unless you have a good reason to make it nested. (The final option is particularly messy, IMO.)

See section 8.1.3 of the JLS for more information about inner classes.

Why am I getting a Non-static variable and static context error?

The problem is your Agenda class is NOT declared as static while your main method is a static method. Try to make your Agenda class static.

Non static field cannot be referenced from a static context- Main method

The main method is static meaning that it belongs to the class and not some object. As such, it a static context cannot reference an instance variable because it wouldn't know what instance of Runner it would use if there even was one.

In short, the solution is to make your Tasks object static in the Runner class.

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

Make the message field static:

static String message;

Your main class is static (as it has to be). This means you don't have an instance of your class. But the field message is currently not static, this means it only exists as part of an instance. Since you don't have an instance you can't access it.

As a side note: I strongly suggest you get someone to code review this code. There is a lot to learn. Consider submitting it to Code Review.



Related Topics



Leave a reply



Submit