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;
}
}
}
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();
}
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.
non-static variable this cannot be referenced from a static context when creating instance of class
You need to make the Edge
nested class static
:
public static class Edge {
...
}
Otherwise, the nested class remains non-static, which means that it retains a reference to the instance of its outer class. As a consequence, only instance methods or other places where you have access to an instance of the outer class can instantiate the inner class.
In general, public static classes are good candidates for top-level classes. The exception is when they are tied to their outer class to the extend that they make no sense outside its context. For example, Map.Entry
makes no sense outside its outer Map
interface.
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 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).
Related Topics
What Is the Priority of Casting in Java
How Is This Private Variable Accessible
Midpoint Between Two Latitude and Longitude
How to Use Mdc with Thread Pools
Highlights Substring in the Tablecell(S) Which Is Using for Jtable Filetering
Parallel Streams, Collectors and Thread Safety
How Much Data Can a List Can Hold at the Maximum
Java - Removing Duplicates in an Arraylist
How to Set Icon in a Column of Jtable
Why Does This Generic Code Compile in Java 8
Getting an Attribute Value in Xml Element
Dbpedia Jena Query Returning Null
Java Http Client Request with Defined Timeout
Why Do We Need Copy Constructor and When Should We Use Copy Constructor in Java
Difference Between Shutdown and Shutdownnow of Executor Service