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);
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.
source share