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.
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();
}
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;
}
}
}
non-static variable arr cannot be referenced from a static context
Your main method is a static method. It actually exists before any object is created from your class, and thus cannot access instance variables and methods directly. You cannot access non static methods or variables from the same class unless you create an object for the class, i.e Launcher launcher = new Launcher();
.
In this case, your player array arr
is not static. You either need to make this static or create a Launcher
object and access the variable from there. In the latter case, you will need to make the arr
array public.
The first option requires you to change your player array declaration to private static Player arr;
.
The second requires you to change the access of the arr
array to public and access it like so: launcher.arr
.
Regarding your second error, you need to either do this: arr = myArray.getPlayerArray();
or just access the array directly like this: myArray.getPlayerArray()[0]
(for the first item in that array).
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.
Related Topics
Why Retrieving Google Directions For Android Using Kml Data Is Not Working Anymore
How to Execute Bash Command With Sudo Privileges in Java
Is Java "Pass-By-Reference" or "Pass-By-Value"
Int Division: Why Is the Result of 1/3 == 0
Converting Iso 8601-Compliant String to Java.Util.Date
How to Read a Large Text File Line by Line Using Java
Round a Double to 2 Decimal Places
How to Properly Stop the Thread in Java
How to Parse Jsonarray in Android
How to Set Java_Home in Linux For All Users
Scanner Is Skipping Nextline() After Using Next() or Nextfoo()
How Do Servlets Work? Instantiation, Sessions, Shared Variables and Multithreading
Change Date Format in a Java String
What Is the Point of the Diamond Operator (≪≫) in Java
How to Check If a String Is Numeric in Java
What Is the Java String Pool and How Is "S" Different from New String("S")