Error "cannot find Symbol" when creating an object

Asked

Viewed 70 times

-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 then javac 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 to import (that is, I would need to know exactly how you are compiling, because this is the cause of the problem - use import in the same package is an unnecessary gambiarra, the ideal is to correct the cause of fact)

2 answers

-2


You must import the class Caneta in the class file Aula02:

package aula02;

import aula02.Caneta;

public class Aula02 {
    // ...
}
  • I imported the class and in Vscode gave the following error: Aula02.java:3: error: cannot find symbol
import aula02.Caneta;
 ^
 symbol: class Caneta
 location: package aula02
Aula02.java:7: error: cannot find symbol
 Caneta c1 = new Caneta();
 ^
 symbol: class Caneta
 location: class Aula02
Aula02.java:7: error: cannot find symbol
 Caneta c1 = new Caneta();
 ^
 symbol: class Caneta
 location: class Aula02
3 errors.

-3

check in Vscode if both the Aula02.java file and the Pen.java file are in Package aula02. No need to give the import aula02.Caneta; because they theoretically are in the same Package.

Browser other questions tagged

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