Basic - Java: error: class, interface, or Enum expected

Asked

Viewed 292 times

-1

I am learning POO by Java and this would be my first code, outside the classic hello world kkk.

How long to compile it via bash, the following error appears:

class2.java:12: error: class, interface, or Enum expected

package aula2;

^ 1 error

From my minimum experience, the error would be in line 12 of the code, with the name "package aula2;", but I cannot understand which error happens in this line...

Follows the code:

package aula2;
public class Aula2 {
public static void main(String[] args) 
{
    Caneta c1 = new Caneta();   
    c1.color = "Blue";
    c1.model = "Bic";
}

}

package aula2;
public class Caneta 
{
    String model;
    String color;
        

    void status()
        {

        System.out.print("Uma caneta " + this.color);
        System.out.print("A marca da caneta é " + this.model);
        
        }
}

Could give me a light to understand kk?

Thanks!

  • 1

    But these are separate files? Declaring the package name twice in the same file is an even error, moreover, you cannot declare two public classes in the same file, as each file can only export one class.

  • I was in doubt to answer you, but I think it’s the same file... as the classes, I didn’t know. The video I saw showed like this. How would you like to move?

2 answers

0

Try to run this way:

 package aula2;

 public class Aula2 {

        public static class Caneta {

            String model;
            String color;

            public void status() {

                System.out.print("Uma caneta " + this.color + ".");
                System.out.print("A marca da caneta é " + this.model);

            }
        }

        public static void main(String[] args) {
            Caneta c1 = new Aula2.Caneta();
            c1.color = "Blue";
            c1.model = "Bic";

            c1.status();
        }
    }

Or this:

package aula2;

public class Aula2 {

    public static void main(String[] args) {
        Caneta c1 = new Caneta();
        c1.color = "Blue";
        c1.model = "Bic";

        c1.status();
    }
}

class Caneta {

    String model;
    String color;

    public void status() {
        System.out.print("Uma caneta " + this.color + ".");
        System.out.print("A marca da caneta é " + this.model);
    }
}

0

You can’t declare packets twice in the same file. Also, only one public class can be declared per file.

Split your class into two files with the same package declaration:

// Arquivo Aula2.java
package aula2;

public class Aula2 {
    public static void main(String[] args) {
        Caneta c1 = new Caneta();
        c1.color = "Blue";
        c1.model = "Bic";
        c1.status();
    }
}

--

// Arquivo Caneta.java
package aula2;

public class Caneta {
    String model;
    String color;

    public void status() {
        System.out.print("Uma caneta " + this.color + ".");
        System.out.print("A marca da caneta é " + this.model);
    }
}

Browser other questions tagged

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