Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static import  RSS feed

 
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)
 
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.
 
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?
 
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(*)?
 
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
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!