• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

question on import statement

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This really makes sense and answers my question. Thanks Sanyal
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic