| Author |
what is static import
|
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
can any body explain me what is static import . If possible can provide code ... thanks a lot .
|
 |
Jeff Bosch
Ranch Hand
Joined: Jul 30, 2003
Posts: 804
|
|
|
Java 1.5 Static Import.
|
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
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
Joined: Jul 11, 2003
Posts: 1392
|
|
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
Joined: Oct 11, 2004
Posts: 3829
|
|
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
Joined: Oct 11, 2004
Posts: 3829
|
|
one more thing Joyce , Why you wrote wildcard in this ... thanks .
|
 |
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
|
|
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
Joined: Mar 14, 2003
Posts: 806
|
|
Hi rathi ji i have sent u private message ..kindly reply it... amit
|
Thanks and Regards,<br />Amit Taneja
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
I have replied !!!
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
Thanks Joycee .... Is it possible to import static method() also .... I think not .. but please reply .. thanks a lot ...
|
 |
Joyce Lee
Ranch Hand
Joined: Jul 11, 2003
Posts: 1392
|
|
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
Joined: Jul 11, 2003
Posts: 1392
|
|
Another example using generic: Filename: Outer2.java Filename: TestOuter.java
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
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 .
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: what is static import
|
|
|