-1
I’m trying to instantiate a new "Pen" object in the main class, but it’s giving an error: "Cannot find Symbol".
- Main class(Aula02):
//pasta em que o código está inserido
package aula02;
//a classe DEVE ser escrita em maiúscula
public class Aula02 {
//Classe principal:
public static void main(String[] args){
//instanciando o objeto Caneta
Caneta c1 = new Caneta();
//adicionando um estado ao atributo cor
c1.cor = "Azul";
//f após o valor -> número real
c1.ponta = 0.5f;
c1.tampada = false;
}
}
Due to "package aula02;", the two codes are in the same folder.
- Class Pen:
//pasta em que se encontra o código
package aula02;
//Declaração da classe
public class Caneta{
//Declarando os atributos da classe
String modelo;
String cor;
float ponta;
int carga;
boolean tampada;
//metódos da classe Caneta
void status() {
System.out.println("Uma caneta" + this.cor);
}
void rabiscar() {
}
void tampar() {
}
void destampar(){
}
}
- Error message:
Aula02.java:4: error: cannot find symbol
Caneta c1 = new Caneta();
^
symbol: class Caneta
location: class Aula02
Aula02.java:4: error: cannot find symbol
Caneta c1 = new Caneta();
^
symbol: class Caneta
location: class Aula02
2 errors
Also, I compiled the "Pen class" before the main class and the same error keeps popping up. Can anyone tell me how to solve this problem?
I followed these exact steps: created the 2 files, compiled
javac aula02/Caneta.java
and thenjavac aula02/Aula02.java
and it worked. Maybe the problem is the way you tried to compile (was in the command line? used some tool? how you tried to compile? etc). Worse is the answer accepted below, which makes no sense, because if two classes are in the same package, it does not need toimport
(that is, I would need to know exactly how you are compiling, because this is the cause of the problem - useimport
in the same package is an unnecessary gambiarra, the ideal is to correct the cause of fact)– hkotsubo