Java import package error -


hi playing around creating java packages.

i created package in folder called admin file called employee - compiles correctly. outside package have java file importing this. here source code.

import java.util.*; // works --> import admin.employee; import admin.*; // doesn't  public class hello {     public static void main(string[] args) {         employee h = new employee("james", 20000);         system.out.println(h.getname());     } } 

the weird thing second import statement works fine third 1 get

  • cannot access employee
  • bad class file: ./employee.class

i using javac hello.java compile

the employee class in package admin. structure is

folder "admin" -> containing "employee.class" , "employee.java" outside folder hello.java file.

package admin; import java.util.*;  public class employee {    private static int nextid;  private int id; private string name = ""; private double salary;  // static initialization block  static {     random generator = new random();     // set nextid random number between 0 , 9999     nextid = generator.nextint(10000); }  // object initialization block {     id = nextid;     nextid++; }  // 3 overloaded constructors public employee(string n, double s) {     name = n;     salary = s; }  public employee(double s) {     // calls employee(string, double) constructor     this("employee #" + nextid, s); }  // default constructor public employee() {     // name initialized ""--see below     // salary not explicityl set--initialized 0     // id initialized in initialization block }   public string getname() {     return name; }  public double getsalary() {     return salary; }  public int getid() {     return id; } } 

package admin;    import java.util.*;  public class employee {   

also employee.java should in directory admin. e.g

./hello.java   ./admin/employee.java 

Comments