Search code examples
javaenumspackageclass-structure

Error - Class interface or enum expected


I am new to java and i am writing this code in notepad which is giving me errors.In netbeans although package is already defined.How to do this in notepad?

package A;
class A {
    private String name;
    protected String company="oracle";  
    A(String name) {
        this.name = name;
        System.out.println(name);
    }
}

public class B extends A {
    // A public class constant
    public final static String st = "Public access modifiers";

    B(String name) {
        super(name);
    }
    void getCompany()
    {
        System.out.println(company);
    }

}

package B;//getting class interface or enum expected 
public class Application {
    public static void main(String[] args) {
        System.out.println(st);
        B b=new B("Java");
        b.getCompany();
    }
}

Solution

  • You can not put different packages into the same source file... You have to create the appropriate folder structure, and separate Java source files for the sources in each package...

    Also, to be able to reference classes from other packages, you have to import them appropriately, and make sure they are actually on the classpath both for compiling and running too......

    Recommended reading