In Java, is it possible to (statically) import constructors or local variables?

474 Views Asked by At

Or can this just be done for methods/fields/enum constants?

2

There are 2 best solutions below

3
On BEST ANSWER

Constructors in Java can't be invoked directly (only via new), so you can't import them apart from the containing class. Since the only use of a constructor, is to create a new instance of the class, you need to import the whole class anyway, and this implies the constructor. Not to mention that if anything, it is an instance method, and you can import only static methods and variables.

Local variables have no existence outside their scope, i.e. they are not tied to a class, only to a code block inside a method [Update](or a static/instance initializer)[/Update]. So how would you import them?

0
On

No, you can't do that.

In order to import method or variable it should be public and static. Constructors and local variables cannot be static.