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

static import

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

How do you import a static method(in this case Integer.tohexString()) rather than using * in import statement?
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot import methods.
Thanks
Deepak
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, you can't import static methods without using wild card(*)?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic