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.
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 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 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 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?
Related Topics
Java Sslhandshakeexception "No Cipher Suites in Common"
How to Use Annotations to Define Different Types of Relationships in Hibernate 4 and Spring
Java Reverse an Int Value Without Using Array
How to Handle Iframe in Selenium Webdriver Using Java
What Data Type to Use for Money in Java
How to Convert Long to Byte[] and Back in Java
String.Replaceall(Regex) Makes the Same Replacement Twice
How to Change UI Depending on Combo Box Selection
Get Width and Height of JPAnel Outside of the Class
How to Get a Unique Computer Identifier in Java (Like Disk Id or Motherboard Id)
Java.Lang.Outofmemoryerror: Gc Overhead Limit Exceeded
Xml Configuration Versus Annotation Based Configuration