Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on import statement

 
Ravi KumarJd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi KumarJd
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really makes sense and answers my question. Thanks Sanyal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic