• 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

static import rules??

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

why this doesn't work?

//import static java.lang.System.out.*; // does Not work
import static java.lang.System.out; // OK

Q: is it because there is no static method/variable in out (PrintStream)?

//import static java.lang.Integer; // does Not work as well
import static java.lang.Integer.*; // OK
import static java.lang.Integer.MAX_VALUE; // OK

import java.lang.Integer; // normal import - work

thanks
anthony
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may import a single static field by name:

import java.lang.Integer.MAX_VALUE;

You may also import all the static fields in a class:

import java.lang.Integer.*;

That's it. The thing to the left of the star must be the name of a class; it can't be the name of a field, like "out" in your example.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So um, before anyone gets the wrong idea here, in both of EFH's previous examples, those should have been "import static" statements, rather than normal-looking "import" statements. Also, static imports can import static methods and even static nested classes (or interfaces), as well as fields.

So, given:

    import static foo.bar.baz;

or

    import static foo.bar.*;

"foo" must be a package, "bar" must refer to a class or interface, and the "baz" or "*" refer to static things - fields, methods, or nested types.

In comparison, given regular imports like

    import foo.bar.baz;

or

    import foo.bar.*;

"foo.bar" must be a package, and "baz" or "*" refer to classes or interfaces.
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim for your clean explanation.

I'm trying to use this import:

import static java.lang.System.out

but why I cannot do this:

println("it's me"); // <b>out.</b>println("it's me");

thanks again.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've imported the class variable "out", that does not mean you imported the instance methods of "out" nor can you. The syntax for using out once you've imported would look like this:



You can't import "println" because it's not static.
[ November 13, 2006: Message edited by: Ken Blair ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


Pardon but that's not accurate. You can import any static member of a class with import static regardless of whether it's a variable, method or nested class.



Did I say otherwise? I said the thing to the left of the star (the asterisk) has to be a class -- i.e., you can say "import all the static things in this class", but you can't say "import all the static things in this object" (whatever that would mean, but it's what the OP was trying to do).
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:


Did I say otherwise? I said the thing to the left of the star (the asterisk) has to be a class -- i.e., you can say "import all the static things in this class", but you can't say "import all the static things in this object" (whatever that would mean, but it's what the OP was trying to do).



I know, I misread and edited, but you replied in the time I did that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic