Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!