In Java, is it possible to (statically) import constructors or local variables?
Or can this jus开发者_运维知识库t be done for methods/fields/enum constants?
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.
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?
精彩评论