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.