Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Imports  RSS feed

 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What is the need of the static imports and when do we need to go for them?

Regards,
Vineela
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
below is why we use static imports

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

above text taken from
http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

hope it helps

cheers!
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have already gone thru the link.

When a class implements an interface, it becomes part of the class's public API. Implementation details should not leak into public APIs.

This part of it is not clear to me


Regards,
Vineela
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All static imports really is, is a shorthand. Earlier people did things
like this:

or

with static imports you can forego the setting of your own variable or
having to prepend the class name to the static variable as follows:

Granted for the above example, this may not be used much, but I can already
see this practice being abused for System.out and similar items, which
will probably make alot of code, alot more confusing.
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class implements an interface, it becomes part of the class's public API. Implementation details should not leak into public APIs.

what this means is
lets say that you create a class and implement an interface which contains few constants that you will be using in the class. now when this class is instantiated the declarations in your interface become public by nature for your class. lets say now you can access the constant from elsewhere with just the object of your class ie obj.cons1.
this is what i think it means "it becomes part of your class's public API"

i hope this helps
cheers!
 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Mohit.I think you got it right.

Thanku.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!