Note that there are some explanatory texts on larger screens.

plurals
  1. POJava import package error
    primarykey
    data
    text
    <p>Hi I am playing around with creating java packages.</p> <p>I created a package in a folder called admin with a file called Employee - this compiles correctly. Outside this package I have another java file that is importing this. Here is the source code.</p> <pre><code>import java.util.*; // this works --&gt; import admin.Employee; import admin.*; // this doesn't public class Hello { public static void main(String[] args) { Employee h = new Employee("James", 20000); System.out.println(h.getName()); } } </code></pre> <p>The weird thing is that the second import statement works fine but with the third one I get</p> <ul> <li>cannot access <code>Employee</code></li> <li>bad class file: <code>./Employee.class</code></li> </ul> <p>I am just using javac Hello.java to compile</p> <p>the employee class is in package admin. The structure is</p> <p>folder "admin" -> containing "Employee.class" and "Employee.java" outside this folder is the hello.java file.</p> <pre><code>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 to a random number between 0 and 9999 nextId = generator.nextInt(10000); } // object initialization block { id = nextId; nextId++; } // three overloaded constructors public Employee(String n, double s) { name = n; salary = s; } public Employee(double s) { // calls the Employee(String, double) constructor this("Employee #" + nextId, s); } // Default constructor public Employee() { // name initialized to ""--see below // salary not explicityl set--initialized to 0 // id initialized in initialization block } public String getName() { return name; } public double getSalary() { return salary; } public int getId() { return id; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload