aspose file tools
The moose likes Beginning Java and the fly likes static import rules?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static import rules??" Watch "static import rules??" New topic
Author

static import rules??

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
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


SCJP 5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
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

Joined: Jul 15, 2003
Posts: 1078
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
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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

Joined: Jul 15, 2003
Posts: 1078
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: static import rules??
 
Similar Threads
imports
Doubt in static import
Static Imports
import
static import