JAVA cannot statically refer to non-static fields

This is my first program in JAVA, and I had a problem to understand this error.

Cannot make a static reference to the non-static field * 

and

Unable to make a static reference to a non-static method *

 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"); r = 5; c_cfr(); c_area(); System.out.ptintln("The cir is: " + cfr); System.out.println("The area is: " + area); } float c_cfr(){ cfr =(float)(2 * pi * r); //casting return cfr; } float c_area(){ area = (float)(pi * (r*r)); return area; } } 

errors Can you give me an offer? I am coding on SandIDE on Android

+6
source share
3 answers

You call instance methods and fields from a static method, which cannot be done because field instances and methods do not exist without an object, and there is no this object inside the main method. Instead, you should instantiate 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; } } 

Many other problems ...

  • You get access to the fields of classes directly, which should not be done. Instead, the fields should be private, and you should use the getters / setters / contructor parameters to get, set, and set the fields.
  • Your code does not make sense, which makes it very difficult to read and understand.

Please find this site because this question has been asked and answered gabillion times, and most likely there is an answer that is much better than mine. If found, then this question should be closed as a duplicate.


Edit
You indicate:

I do not understand. Instead, the fields should be private, and you should use the getters / setters / contructor parameters to get, set, and set the fields. "Should I write private float c_cfr ()?

Your fields:

 float r; float area; float cfr; 

This is really not a field, but a constant: a finite double pi = 3.14;

and can be replaced / improved by simple use of Math.PI.

Your fields must be changed:

 private float r; private float area; private float cfr; 

and you should only access them using the public getter and setter methods, and only if absolutely necessary.

+17
source

A simple fix is ​​to put the word static before each method. This is the universal static circle of truth = 2pi * r, your circle may be larger than my circle (both instances of the circle), but there is one formula to find the area

0
source

c_cfr () and c_area () are non-static methods that you are trying to call directly from the static main method. Either make the c_cfr () and c_area () methods too static, or access them with an object reference.

0
source

Source: https://habr.com/ru/post/956371/


All Articles