aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "static import" Watch "static import" New topic
Author

static import

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134


in the above code as the static memeber, java.lang.System.out has been already imported, why 'out' is still used in out.println(MAX_VALUE) rather than println(MAX_VALUE)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Because println() is a method of an object refered by the static variable out.
println is not a method of class System.


Bu.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

java.lang.System.out has been already imported, why 'out' is still used in out.println(MAX_VALUE) rather than println(MAX_VALUE)


You import the static member of the class using "import static";
out is static member of the class System. And out is object of PrintStream,
using out you access the public methods like print(...), println() of
the PrintStream class.


Thanks,


cmbhatt
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
Thanks guys.

How do you import a static method(in this case Integer.tohexString()) rather than using * in import statement?
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
You cannot import methods.
Thanks
Deepak


SCJP, SCWCD, SCBCD
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
You mean, you can't import static methods without using wild card(*)?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by jibs parap:
How do you import a static method
The same way you use to import a static variable.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Thanks for the clarification.
Yes we can import static variable or method using static import.
To import all the static members [Variables+methods] use pkg.classname.*
For example to import all the static members of Arrays clas
import static java.util.Arrays.*;

To import static method sort() use
import static java.util.Arrays.sort;

To import static variable
import static classname.staticVariableName;

Thanks
Deepak
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static import