Can import statements be used anywhere besides the beginning of a Java file?
Asked Answered
S

5

7

In python, the import statement can be placed everywhere in the file, even inside a class, or an if.

Is there a way to accomplish the same thing in Java? I understand that it could be a bad practice not to put all the imports at the top of the file, I'm just wondering if it is possible in some way or not.

Slide answered 26/1, 2016 at 11:4 Comment(0)
P
10

The very first statement in a Java file must be (if there is one) the package statement, followed by the import statements. They can not be placed in another location.

However, it is possible to skip the import altogether by using fully qualified class names (which I personally don't recommend). You need to use them everywhere you would have used the short, unqualified name.

import my.package.MyClass;

public class Test{
    private MyClass instance = new MyClass();
}

can be rewritten as:

public class Test{
    private my.package.MyClass instance = new my.package.MyClass();
}
Piercy answered 26/1, 2016 at 11:9 Comment(0)
S
3

According to the documentation here:

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one.

So it seems that it is not possible.

Slide answered 26/1, 2016 at 11:6 Comment(0)
P
2

Short answer : No it's impossible !

The import statement must be in the top of the file after the package statement (if exist).

You must know : You can use your imported class/interface or static method in all classes/interfaces in the same file including inner/nested classes.

Pusey answered 26/1, 2016 at 11:11 Comment(0)
S
0

There isn't a way, except maybe messing with bytecode if you count that. I suppose the best equivalent would be writing the fully qualified name of what you're looking to use.

Not sure why you would want to though.

Starveling answered 26/1, 2016 at 11:7 Comment(1)
import is only a shortcut for the compiler - in the class file, everything is fully qualified.Chamonix
S
0

No. They need to be at the top, after the package declaration.

An ordinary compilation unit consists of three parts, each of which is optional:

  • A package declaration (§7.4), giving the fully qualified name (§6.7) of the package to which the compilation unit belongs.

    A compilation unit that has no package declaration is part of an unnamed package (§7.4.2).

  • import declarations (§7.5) that allow classes and interface from other packages, and static members of classes and interfaces, to be referred to using their simple names.

  • Top level declarations of classes and interfaces (§7.6).

This doesn't do a great job at conveying that the ordering of each part is strictly enforced, but the formal grammar does make this clear:

OrdinaryCompilationUnit:
    [PackageDeclaration] {ImportDeclaration} {TopLevelClassOrInterfaceDeclaration}
Sagittal answered 11/5, 2021 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.