helix
helix

Reputation: 1056

why java allows importing a class defined in same file?

I created a java file named TestClass.java and put it inside a folder testPackage. On compilation, importing the same class which I defined in this same file, didn't give me any error.

Why is it allowing to do so ? Is there any real purpose behind this or just a grammar glitch, the impact of which is too low (because of bad practice?) that nobody cares (like more than one ; after import statement)?

package testPackage;
import testPackage.TestClass;
public class TestClass{
    public static void main(String args[]){
        System.out.println("hello world");

    }
}

I'm using the below setup :

C:\test>tree /F
Folder PATH listing for volume OS
Volume serial number is 2A49-80E2
C:.
└───testPackage
        TestClass.java

C:\test>javac -version
javac 1.8.0_181

C:\test>java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

C:\test>javac -cp . testPackage\TestClass.java

C:\test>java testPackage.TestClass
hello world

Upvotes: 1

Views: 1057

Answers (2)

Patrick Anderson
Patrick Anderson

Reputation: 73

As far as I know the compiler profiles the class to be compiled and all imported classes and ensures that multiple different profiled classes don't have the same name in the given scope(ie different files aren't importing different files with classes with the same name). It is highly common for multiple files to import the same class (or same file) and thus this situation is not "incorrect" in that aspect. In such a case the compiler would simply note that it has read a class and so the next time it is included, it realizes that it has already read that class and uses the existing profiled class. After the compiler has profiled each class (likely pre forming the class hierarchy) it goes through and compiles the classes it has profiled.

This method should give the same result as if the given case was handled explicitly but the code is simpler thus leaving less room for errors and increasing performance as well as possibly handling other "strange" situations which may otherwise have result in incorrect compilation due to having to specifically cater for them.

many compilers avoid these situations like the plague as they do not do this profiling step and thus each time situations like this happen they run the risk of causing the compiler to enter an infinite loop as they continually restart compiling the same file each time as they see it as a dependency and thus must be compiled first before they can compile the original (and same) file.

Upvotes: 1

ernest_k
ernest_k

Reputation: 45309

There are cases where it's necessary to import a class defined in the same file. Your example surely isn't one of them (your import is simply superfluous because classes in the same package are in the same namespace, which roughly means that classes in the same package are implicitly imported).

But look at this (suppose all code is in the same file):

package stackoverflow;

import stackoverflow.OtherClass.OtherClassInner;

public class Main {
    public static void main(String[] args) throws Exception {
        OtherClassInner inner = new OtherClassInner();
    }
}

class OtherClass {
    static class OtherClassInner {

    }
}

In this case, Main is not in the same namespace as OtherClassInner, so it either has to fully qualify stackoverflow.OtherClass.OtherClassInner (or reference it through OtherClass.OtherClassInner as Main and OtherClass don't need to import one another), or to import it as shown in the above snippet.

And just to make it obvious why the import is necessary, consider this (where there are two OtherClassInner in the same file):

public class Main {
    public static void main(String[] args) throws Exception {
        OtherClassInner inner = new OtherClassInner();
    }
}

class OtherClass {
    static class OtherClassInner {

    }
}

class YetAnotherClass {
    static class OtherClassInner {

    }
}

Upvotes: 3

Related Questions