Non-static method cannot be referenced from static context?

Asked

Viewed 1,621 times

2

I was watching the programming class in java and basically the business was to calculate the total area of a house with a pool, however, I’m having difficulties. Basically I separated everything into three files, being one of them the main one, but when I try to compile, appears the message saying:

non-static method cannot be referenced from a static context

Areacasa.java:

public class AreaCasa {
    //preço do metro quadrado
    double valorM2 = 1500;

    //calcula a área da casa
    double CasaRet(double l_sala, double c_quarto) {
        double area_s; //área da sala
        double area_q; //área do quarto
        double area_t = 0; //área do total

        if(l_sala < 0 || c_quarto < 0)
             System.out.println("Erro!");

        else{
            area_s = l_sala * l_sala; //calcula area da sala
            area_q = c_quarto * (l_sala/2); //calcula area do quarto            
            area_t = area_s + 2 * area_q; //calcula a area total
        }
        return(area_t);
    }
}

Areapiscina.java:

public class AreaPiscina {
    double AreaPiscina(double raio){ 
        return((raio >= 0) ? Math.PI * Math.pow(raio, 2) : -1);
    }
}

Project.java (is the main file):

public class Projeto {
    double Area(double lateral_1, double lateral_2, double pis_raio) {
        return(AreaCasa.CasaRet(lateral_1, lateral_2) + AreaPiscina.AreaPiscina(pis_raio));
    }

    public void main (String args[]) {
        System.out.println(Area(21.43, 33.4, 2.0));
    }
}

When I try to compile this last file appears:

[user@localhost TesteJava]$ javac Projeto.java
Projeto.java:3: error: non-static method CasaRet(double,double) cannot be referenced from a static context
    return(AreaCasa.CasaRet(lateral_1, lateral_2) + 
                   ^
Projeto.java:4: error: non-static method AreaPiscina(double) cannot be referenced from a static context
    AreaPiscina.AreaPiscina(pis_raio));
               ^
 2 errors

I can’t understand why it’s giving error, the functions within the design class are not even static (I think)! I think I did everything right by passing the values by reference to another function.

  • Exactly that! You’re trying to access non-static methods in a static way.

  • How so static? The terminal did inform me, but how can I render the functions of the.java Project class into non-static?

2 answers

3


You are trying to access non-static methods statically.

In doing AreaCasa.CasaRet(lateral_1, lateral_2) + AreaPiscina.AreaPiscina(pis_raio); you are calling the methods of the classes AreaCasa and AreaPiscina statically (Class.method) without the methods being static (identified by static), not to mention you’re trying to give a return in a builder.

To use the methods the way you wrote them, you need to instantiate the classes before using your methods:

Note: The constructor does not return values, so you need to create one method other than class name AreaPiscina.

    public class AreaPiscina {

    double calcularArea(double raio){ 
      return((raio >= 0) ? Math.PI * Math.pow(raio, 2) : -1);
    }

}

And then make the call:

 double Area(double lateral_1, double lateral_2, double pis_raio) {
         AreaCasa casa = new AreaCasa();
         AreaPiscina piscina = new AreaPiscina(); 
        return(casa.CasaRet(lateral_1, lateral_2) + piscina.calcularArea(pis_raio));
    }

And in the main class:

public void main (String args[]) {
    Projeto p = new Projeto();
    System.out.println(p.Area(21.43, 33.4, 2.0));
   }

See working on ideone.


Another solution would be to change the classes and methods to static:

Class Areapiscina:

    public static class AreaPiscina {

    static double calcularArea(double raio){ 
      return((raio >= 0) ? Math.PI * Math.pow(raio, 2) : -1);
    }

}

Classe Areacasa:

public static class AreaCasa {
    //preço do metro quadrado
    static double valorM2 = 1500;

    //calcula a área da casa
    static double CasaRet(double l_sala, double c_quarto) {
        double area_s; //área da sala
        double area_q; //área do quarto
        double area_t = 0; //área do total

        if(l_sala < 0 || c_quarto < 0)
             System.out.println("Erro!");

        else{
            area_s = l_sala * l_sala; //calcula area da sala
            area_q = c_quarto * (l_sala/2); //calcula area do quarto            
            area_t = area_s + 2 * area_q; //calcula a area total
        }
        return(area_t);
    }
}

Your Design class and method Area:

public class Projeto {
 static double Area(double lateral_1, double lateral_2, double pis_raio) {
        return(AreaCasa.CasaRet(lateral_1, lateral_2) + AreaPiscina.calcularArea(pis_raio));
    }

    public static void main (String[] args) {
      System.out.println(Area(21.43, 33.4, 2.0));
    }
}

Functioning in the IDEONE.

Here are some links from SOPT for reading:

What is the function of a static method?

What is the use of a static or final variable in java?

0

static double CasaRet(double l_sala, double c_quarto) {
    double area_s; //área da sala
    double area_q; //área do quarto
    double area_t = 0; //área do total

    if(l_sala < 0 || c_quarto < 0)
         System.out.println("Erro!");

    else{
        area_s = l_sala * l_sala; //calcula area da sala
        area_q = c_quarto * (l_sala/2); //calcula area do quarto            
        area_t = area_s + 2 * area_q; //calcula a area total
    }
    return(area_t);
}

All we had to do was add the static in the method. Otherwise you should instantiate a class object and so call the method.

AreaCasa ac = new AreaCasa();
ac.CasaRet(valor1, valor2);
  • This does not fully solve the question problem. The class AreaPiscina is also having an equivocal referenced method, and this only deals with the Areacasa class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.