Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is static import

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body explain me what is static import . If possible can provide code ...

thanks a lot .
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.5 Static Import.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never though that it would be such a simple thing .




both code are doing same thing , but in second case all the variable ( PI & E ) are being created locally for My class or in other word , we have a local copy of all the variable in My class .

Is this right ... and sufficient for beta ....

thanks a lot .
[ February 02, 2005: Message edited by: rathi ji ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There're few flaws in the code:
1. It should be import static
2. To import a static member like PI in Math, it should be

or


So be careful, don't get mixed up with normal import.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means , If we are importing a class then we will use * sign & If we are importing particular member ( variable only , method is not possible I think ) then we will not use * sign .

Is this right ?

Please reply .

Thanks a lot .
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing Joyce , Why you wrote wildcard in this ...


thanks .
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wildcard allows importing all the static members of Math class in one statement. Let's say, you want to import some static members like PI, min and max in Math, there are two approaches to do this:



or simply

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rathi ji

i have sent u private message ..kindly reply it...


amit
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have replied !!!
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joycee ....

Is it possible to import static method() also .... I think not .. but please reply ..

thanks a lot ...
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to import static method() also .... I think not .. but please reply ..

Yes, it's possible. max and min are static methods in Math class. You can also import a static nested class.

Here is an example:

Filename: StaticClass.java


Filename: TestStatic.java


Try compile the code and see the result.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example using generic:

Filename: Outer2.java


Filename: TestOuter.java
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Joycee , How can you create an instance on static inner class without specifying outer class name .
I think the correct code should be this :


please reply ...

thanks .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic