3

I'm just curious about something. Say I have the following environment:

Folder structure -

projects \ prj1 \ src \ pkgs \ main (contains Main.java)
                             \ test (contains TestClass.java)

Main.java -

package pkgs.main;

import pkgs.test.TestClass;

class Main {
    public static void main(String[] args) {
        TestClass tc;
    }
}

Java shell based compilation commmands -

javac -d ..\cls -sourcepath ..\src ..\src\pkgs\main\Main.java
java -classpath ..\cls pkgs.main.Main

If I then make a .java code file called Default.java, that doesn't have a package statement -

public class Default {}

and place this file in the src folder, how can a class file such as Main.java access this Default class? I tried using the following import statements, but neither worked, causing compilation errors:

Main.java -

package pkgs.main;

// import Default;    (causes many compilation error messages)
// import .Default;   (causes "identifier expected" compilation error)

I also moved the Default.java file from the src folder, in to the same folder that contained Main.java, but that didn't work either. I appreciate that this whole idea seems like bad design, but I'm only doing it in order to learn a bit more about how the package and import statements work, alongside the project folder structure. Thanks.

user2911290
  • 1,396
  • 1
  • 16
  • 26
  • 1
    Have you tried just using `Default` *without* importing it? That won't work if `Default` is also in one of the packages you're importing, but I don't think there's any way round that - just another reason to *always* use a package. – Jon Skeet May 06 '14 at 12:00
  • 5
    As soon as you give a class a package, any class not explicitly imported will be assumed to be in the same package. So no, a class in the default package simply becomes "invisible" and I can't think of a way that you would be able to get around that. Nor why you would want to. – Gimby May 06 '14 at 12:06
  • @Jon Skeet, I did try that; the error message displayed is "cannot find symbol". Also, I tried moving the Default.java class file in to the same folder as Main.java, but that generated a "cannot access Default / bad source file" error message. – user2911290 May 06 '14 at 12:06
  • 1
    Interesting - I'd *expected* that to work, but if it doesn't, I'd just say you need to use a package for `Default`. That's going to be a lot simpler than trying to get this to work, IMO. I wouldn't spend time worrying about a situation which should be avoided anyway. – Jon Skeet May 06 '14 at 12:09
  • 1
    @Jon Skeet, for sure. I was just trying this out for some learning XP ;) – user2911290 May 06 '14 at 12:13
  • You can find the answer to your problem in this question (i. e. your question is a duplicate): http://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package and also here: http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package the first question has a better answer in my opinion – morgano May 06 '14 at 12:18

1 Answers1

2

To avoid two steps of indirection, the essence of the accepted answer is that according to the specification:

It is a compile time error to import a type from the unnamed package.

Which means that if you're not in the unnamed (aka default) package, you can't reach classes in the unnamed/default package, unless you are prepared to turn to reflection.

However, if you're in the unnamed/default package, you don't need to import anything, just don't forget to set the classpath, e.g. javac -cp "." ABC.java

Community
  • 1
  • 1
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124