cannot make a static reference to the non-static field
the lines
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
you might want to make withdraw and deposit non-static and let it modify the balance
public void withdraw(double withdrawAmount) {
balance = balance - withdrawAmount;
}
public void deposit(double depositAmount) {
balance = balance + depositAmount;
}
and remove the balance parameter from the call
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.
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?
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.
Java-Cannot make a static reference to the non-static field
You probably intended to access the instance variable of the instance you created :
phone xy = new phone();
int y = xy.x;
Since x
is not a static variable it can't be accessed without specifying an instance of the phone
class.
Of course this will also fail, unless you change the access level of x
to public
(which is possible but not advisable - you should use getter and setter methods instead of directly manipulating instance variables from outside the class).
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();
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) {
...
}
Related Topics
Mockito - Difference Between Doreturn() and When()
How to Determine the Class of a Generic Type
Memory Address of Variables in Java
Eclipse 2021-09 Code Completion Not Showing All Methods and Classes
Inject an Ejb into Jax-Rs (Restful Service)
Java Random Always Returns the Same Number When I Set the Seed
What Is the Use of Interface Constants
Get Width and Height of JPAnel Outside of the Class
How to Ask the Selenium-Webdriver to Wait for Few Seconds in Java
Best Practice for Setting Jframe Locations
How to Ignore Ssl Certificate Errors in Apache Httpclient 4.0
How to Avoid Floating Point Precision Errors with Floats or Doubles in Java
@Requestbody and @Responsebody Annotations in Spring
Strange Floating-Point Behaviour in a Java Program
Converting Symbols, Accent Letters to English Alphabet
How to Use Regular Expressions to Parse HTML in Java