Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
static imports
Konda Golamaru
Ranch Hand
Posts: 36
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Pls. clarify on below static imports.
import static java.lang.*;//generates the compilation error
cannot find symbol class lang
import static java.lang.System.out;//works fine
Keith Lynn
Ranch Hand
Posts: 2412
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The static import feature can only be used to import the static members of a class. You cannot use it the way you use a normal import statement where you import all .class files in a package.
ragavendran krishnamoothy
Greenhorn
Posts: 17
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
can you explain the static import concepts with some examples ..........
stating the advantages & restrictions of the concepts....
Apna Apana
Greenhorn
Posts: 20
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi
can you please explain in detail about static imports and also how they are useful.
Cheers
Apna
Keith Lynn
Ranch Hand
Posts: 2412
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is an example of using a static import.
import static java.lang.Math.pow; class MyClass { public double e(int number) { return(pow(1+1.0/number,number)); } }
As you can see the advantage of using the static import is that we do not have to use the classname Math when calling the method pow.
We can call the method as if it were defined in the class.
Apna Apana
Greenhorn
Posts: 20
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank You Keith
Nik Arora
Ranch Hand
Posts: 652
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import static java.lang.*;//generates the compilation error
cannot find symbol class lang
Hi Konda,
You cannot import all the members by saying "*" on the package.You have to explicitly import all the members.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Instantiation of a static nested class
Import static
Error trying to get a simple bare-bones package to work.
static imports
importing from same name packages
More...