Non-static method can not be referenced from static context?

2

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

  

Non-static method can not 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 (this 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 it 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 not understand why it is giving error, the functions inside the project class are neither static (I think)! I think I did everything right by passing the values by reference to another function.

    
asked by anonymous 06.02.2016 / 21:47

2 answers

3

You are trying to access non-static methods in a static way.

By doing AreaCasa.CasaRet(lateral_1, lateral_2) + AreaPiscina.AreaPiscina(pis_raio); you are calling the methods of the AreaCasa and AreaPiscina classes in static (Class.method) without the methods being static (identified by static ), not counting that you are trying to give a return in a constructor.

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

  

OBs: The constructor does not return values, so you need to create a   method with different name of class 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 at ideone .

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

Pool Area Class:

    public static class AreaPiscina {

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

}

Class 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 project 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));
    }
}

Running on IDEONE .

Here are some links from here to SOPT for reading:

What is the function of a static method?

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

    
06.02.2016 / 22:00
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);
}

Just missing the static in the method. Otherwise you must instantiate an object of the class and thus call the method.

AreaCasa ac = new AreaCasa();
ac.CasaRet(valor1, valor2);
    
06.02.2016 / 22:26