Cannot make a static reference to the non-static method fxn(int) from the type Two
Since the main
method is static
and the fxn()
method is not, you can't call the method without first creating a Two
object. So either you change the method to:
public static int fxn(int y) {
y = 5;
return y;
}
or change the code in main
to:
Two two = new Two();
x = two.fxn(x);
Read more on static
here in the Java Tutorials.
Cannot make a static reference to the non-static method
Since getText()
is non-static you cannot call it from a static method.
To understand why, you have to understand the difference between the two.
Instance (non-static) methods work on objects that are of a particular type (the class). These are created with the new like this:
SomeClass myObject = new SomeClass();
To call an instance method, you call it on the instance (myObject
):
myObject.getText(...)
However a static method/field can be called only on the type directly, say like this:
The previous statement is not correct. One can also refer to static fields with an object reference like myObject.staticMethod()
but this is discouraged because it does not make it clear that they are class variables.
... = SomeClass.final
And the two cannot work together as they operate on different data spaces (instance data and class data)
Let me try and explain. Consider this class (psuedocode):
class Test {
string somedata = "99";
string getText() { return somedata; }
static string TTT = "0";
}
Now I have the following use case:
Test item1 = new Test();
item1.somedata = "200";
Test item2 = new Test();
Test.TTT = "1";
What are the values?
Well
in item1 TTT = 1 and somedata = 200
in item2 TTT = 1 and somedata = 99
In other words, TTT
is a datum that is shared by all the instances of the type. So it make no sense to say
class Test {
string somedata = "99";
string getText() { return somedata; }
static string TTT = getText(); // error there is is no somedata at this point
}
So the question is why is TTT static or why is getText() not static?
Remove the static
and it should get past this error - but without understanding what your type does it's only a sticking plaster till the next error. What are the requirements of getText()
that require it to be non-static?
Method Reference. Cannot make a static reference to the non-static method
Method references to non-static methods require an instance to operate on.
In the case of the listFiles
method, the argument is a FileFilter
with accept(File file)
. As you operate on an instance (the argument), you can refer to its instance methods:
listFiles(File::isHidden)
which is shorthand for
listFiles(f -> f.isHidden())
Now why can't you use test(MyCass::mymethod)
? Because you simply don't have an instance of MyCass
to operate on.
You can however create an instance, and then pass a method reference to your instance method:
MyCass myCass = new MyCass(); // the instance
test(myCass::mymethod); // pass a non-static method reference
or
test(new MyCass()::mymethod);
Edit: MyCass
would need to be declared static (static class MyCass
) in order to be accessible from the main method.
JAVA - Cannot make a static reference to the non-static method
In order to use the User
method in a static context (main
method for the example), you need to instantiate the lookFor class and call the User
method on that object :
lookFor look = new lookFor(); // Use appropriate constructor
if(look.User(username, users) == -1) {
...
}
cannot make a static reference to a non static method
A static method belongs to the class, a non-static method belongs to an instance of the class.
When you call getCurrentValue()
from main
, you get an error because main
isn't associated with any instance.
You need to create an instance of the class:
HallLanceMemoryCalculator me = new HallLanceMemoryCalculator();
Then you can call the instance's getCurrentValue()
:
double value = me.getCurrentValue();
Cannot make a static reference to the non-static type T
That makes sense. Think about what the T
means - you are defining a class where each instance specifies its generic concrete type.
However static members belong to the class and not a specific instance. when you define a static member in it, that depends on such T
what will it be? Static has no instance - so what T
.
If you want the queue
to be a member of the instance, and depend on T
just remove the static
:
public class BlockingQueue<T> {
public List<T> queue = new ArrayList<T>();
}
How to solve Cannot Make Static Reference to Non-Static Method in a Java mastermind game?
You can't call a non-static method directly from a static method. in public static main(String [] args)
To do so, you should first create an object of the class.
try this at main method:
mm m1 = new mm();
random = m1.numberGenerator();
int [] guess = m1.userInput();
this should work
JAVA cannot make a static reference to non-static field
You are calling instance methods and fields from within a static method, something that can't be done because instance fields and methods don't exist without an object, and inside of the main method there is not this
object. You must instead create an instance of the class, and then call the methods on the instance.
public class Cerchio{
float r;
float area;
float cfr;
final double pi = 3.14;
public static void main(String[] args){
System.out.println("CIRCLE PROGRAM\n");
Cerchio cerchio = new Cerchio();
cerchio.r = 5;
cerchio.c_cfr();
cerchio.c_area();
System.out.ptintln("The cir is: " + cerchio.cfr);
System.out.println("The area is: " + cerchio.area);
}
float c_cfr(){
cfr =(float)(2 * pi * r); //casting
return cfr;
}
float c_area(){
area = (float)(pi * (r*r));
return area;
}
}
Lots of other problems,...
- You're accessing class fields directly, something that shouldn't be done. Instead, the fields should be private and you should use getters/setters/contructor parameters to get, set and set the fields.
- Your code is unindented making it very hard to read and understand.
Please search this site as this same question has been asked and answered a gabizillion times, and most likely there's an answer out there that is much better than mine. If found, then this question should be closed as a duplicate.
Edit
You state:
I didn't understand "Instead, the fields should be private and you should use getters/setters/contructor parameters to get, set and set the fields." I should write private float c_cfr() ?
Your fields are:
float r;
float area;
float cfr;
This is really not a field but a constant:
final double pi = 3.14;
and can be replaced / improved by simply using Math.PI.
Your fields should be changed to:
private float r;
private float area;
private float cfr;
and you should only access them via public getter and setter methods, and only if absolutely necessary.
how to solve cannot make a static reference to the non-static method?
(If your class name is Main
then) use Main.class.getResource
instead of this.getClass.getResource
Read this for more details.
Related Topics
How to Stop Repeated Keypressed()/Keyreleased() Events in Swing
Running a Jar File Without Directly Calling 'Java'
Limit Jvm Process Memory on Ubuntu
How to Run a Command At Terminal from Java Program
How to Execute Python Script from Java (Via Command Line)
What's the Difference Between JavaScript and Java
Difference Between Jsonobject and Jsonarray
How to Ping External Ip from Java Android
Error: Could Not Find or Load Main Class
Running Jmap Getting Unable to Open Socket File
How to Execute Bash Command With Sudo Privileges in Java
Growing Resident Memory Usage (Rss) of Java Process
React on Global Hotkey in a Java Program on Windows/Linux/Mac
Gradle Finds Wrong Java_Home Even Though It's Correctly Set
Javafx on Linux Is Showing a "Graphics Device Initialization Failed For: Es2, Sw"