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.
– user28595
How so static? The terminal did inform me, but how can I render the functions of the.java Project class into non-static?
– Carlos Giovano