• Post Reply Bookmark Topic Watch Topic
  • New Topic

static import rules??  RSS feed

 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!