| Author |
question on import statement
|
Ravi KumarJd
Greenhorn
Joined: Feb 04, 2012
Posts: 4
|
|
Hi -
I know we can use static import to for static methods and variables. I tried to import instance members in the same fashion static import and as expected it gave compiler error. But when use this import package2.TestClass.*; compiler didn't complain anything and I'm not able to the import class even. Any specific use with this kind of import package.class.* ?
Regards,
Ravi
|
 |
Vadim Vararu
Ranch Hand
Joined: Jan 03, 2009
Posts: 147
|
|
Hi!
Static import is used only and only for static things. You can import static methods or static attributes. With static import you can not import anything else. You can not import instance methods or attributes. That's the rule and you have to memorize it.
With the non static import you can import just concrete Types (for instance: import java.util.List) or all the types from a certain package (for instance: import java.util.*).
There are no other ways to use import. You can not import instance members or methods.
Have a good day, Vadim.
|
If you think you've done too much, usually it means you've done too few.
|
 |
Ravi KumarJd
Greenhorn
Joined: Feb 04, 2012
Posts: 4
|
|
|
Great answer - Thank you. I agree with you we cannot import instance methods or attributes directly with import statement. I wondered why the compiler not stopping me, when I append .* after a concrete class. If I understand correctly, for non static concrete Types/classes it's NOT better to append .* after the class name though the compiler okay.
|
 |
Sebanti Sanyal
Ranch Hand
Joined: Nov 07, 2011
Posts: 58
|
|
Ravi KumarJd wrote:Great answer - Thank you. I agree with you we cannot import instance methods or attributes directly with import statement. I wondered why the compiler not stopping me, when I append .* after a concrete class. If I understand correctly, for non static concrete Types/classes it's NOT better to append .* after the class name though the compiler okay.
If TestClass contained a static nested class or an enum, you could have imported them (as types) with import package2.TestClass.*;
|
 |
Ravi KumarJd
Greenhorn
Joined: Feb 04, 2012
Posts: 4
|
|
|
This really makes sense and answers my question. Thanks Sanyal
|
 |
 |
|
|
subject: question on import statement
|
|
|