Mitro
Mitro

Reputation: 1260

JAVA cannot make a static reference to non-static field

this is my first program in JAVA and I'm having problem to understand this error

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

and

Cannot make a static reference to the 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 any suggest? I'm coding on SandIDE on Android

Upvotes: 8

Views: 70309

Answers (4)

user18491985
user18491985

Reputation: 1

In this code you are trying to acess non static instances and methods of class in static method i.e public static void main(String[] args)

so,change instances and methods to static or create the object of Cerchio and then acess them

public class Cerchio {

       static float r;
       float area;
       float cfr;
       
       static  float c_cfr() {
          return (float)(2 * Math.PI* r);
       }

       float c_area() {
          area = (float)(Math.PI * (r*r));
          return area;
       }

       public static void main(String[] args){
          System.out.println("CIRCLE PROGRAM\n");
          Cerchio.r = 5;
       
          Cerchio s = new Cerchior();
          
          System.out.println("The cir is: " +c_cfr());
          System.out.println("The area is: " + s.c_area());
       }
}

Upvotes: 0

Vivek Vermani
Vivek Vermani

Reputation: 2004

c_cfr() and c_area() are non static methods which you are trying to call directly from the static main method. Either make the methods c_cfr() and c_area() as static too or access them using object reference.

Upvotes: 0

David Allan Houser Jr
David Allan Houser Jr

Reputation: 127

The simple fix is to put the word static in front of each method. It is a universal static truth circumference =2pi*r your circle may be bigger than my circle (both instances of circle) but to find the area there is one formula

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 20

Related Questions