File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is static import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is static import" Watch "what is static import" New topic
Author

what is static import

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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: 805
Java 1.5 Static Import.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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: 3830
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: 3830
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: 810
Hi rathi ji

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


amit


Thanks and Regards, Amit Taneja
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I have replied !!!
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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: 3830


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: http://aspose.com/file-tools
 
subject: what is static import