IMPORT STATIC; Why is "import static" file scope? Why not class scope?

P

Paul Opal

Why do we write:

import static BorderLayout.*;
class MyStuff {
...
}

instead of writing:

class MyStuff {
import static BorderLayout.*;
...
}

..... I thing of specific use, of course: Say I have inner classes and
want the import to be only for one inner class.

Cheers!
 
T

Tor Iver Wilhelmsen

import static BorderLayout.*;

Import statements go in a special "section" before the class
declarations as the language is currently defined. Other languages
(esp. Python) lets you import names anywhere in the code, but Java
doesn't. That's why.
 
D

Dave Glasser



Since they started talking about the new features in 1.5 a year or so
ago, I've had this nagging feeling that Java is following in the
footsteps of C++, becoming overloaded with syntactic-sugar features
that may save a few keytrokes here and there, but don't really make
the language more powerful and often make the code less readable. Even
the page you linked says "if you overuse the static import feature, it
can make your program unreadable and unmaintainable, polluting its
namespace with all the static members you import."

So is it worth having it in there, even if, as they say, "used
appropriately, static import can make your program more readable, by
removing the boilerplate of repetition of class names?"

Personally, I'd rather see Sun devote their efforts to fixing the bugs
in Swing than shoveling stuff like this into the language.


--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net
 
S

Sudsy

Dave said:
Sudsy <[email protected]> wrote on Sat, 09 Oct 2004 14:17:29
So is it worth having it in there, even if, as they say, "used
appropriately, static import can make your program more readable, by
removing the boilerplate of repetition of class names?"

FWIW I wasn't recommending the practice and agree that a slight
increase in readability is poor justification for including such
a "feature".
I was merely addressing Andrew's point about the code not compiling
and pointing him to the documentation. I've seen the construct used
by a couple of posters recently and had to educate myself on the
syntax, being that it's new to 1.5.
Just trying to help...
 
A

Andrew Thompson

O

Oscar kind

Andrew Thompson said:
And I'm still wonderring how you compile it..

(shrugs) I have 1.5, but javac still whines plaintively
at that code sample.

Probably because by default it doesn't use the 1.5 features. After all, to
use the assert keyword with 1.4 you also had to tell the compiler to
accept it.

I expect that a switch "-source 1.5" is needed.

If you already used that switch, I don't know why it doesn't compile (I
haven't played with the new language "features" yet).
 
C

Chris Uppal

Dave said:
Since they started talking about the new features in 1.5 a year or so
ago, I've had this nagging feeling that Java is following in the
footsteps of C++, becoming overloaded with syntactic-sugar features
that may save a few keytrokes here and there, but don't really make
the language more powerful and often make the code less readable.

That's my feeling too.

Although I don't blame Sun for this, except insofar as it's because they are
listening to what the community says it wants.

-- chris
 
A

Andrew Thompson

X

xarax

Chris Uppal said:
That's my feeling too.

Although I don't blame Sun for this, except insofar as it's because they are
listening to what the community says it wants.

I can only imagine how much worse it would be
if Sun didn't exercise strong control over the
language. Ceding control of the Java language
by putting into a public community process (without
a veto by Sun) would cause enormous harm to the language.

A camel is a horse designed by a committee.
 
C

Chris Uppal

xarax said:
I can only imagine how much worse it would be
if Sun didn't exercise strong control over the
language. Ceding control of the Java language
by putting into a public community process (without
a veto by Sun) would cause enormous harm to the language.

A camel is a horse designed by a committee.

I don't think a camel is exactly what we/they'd come up with.

I have a picture in my mind of a horse with added safety:
-- seats afixed directly to its spine..
-- training wheels emerging from its ribs.
-- ...

PLUS, performance improvements:
-- a V8 engine bolted under it stomach (which it can't use).
-- an optional mode where it turns into a cheeta (but eats its rider).
-- ...

PLUS, convenience features:
-- ladders up both sides.
-- GPS navigation.
-- ...

MINUS (if minimalists like me got our way -- which we wouldn't):
-- one leg (after all, three legs is more stable, right ?).
-- the mane and tail (merely decorative...).
-- ...

Poor beast.

-- chris
 
P

Paul Opal

Thx.

Tor Iver Wilhelmsen said:
Import statements go in a special "section" before the class
declarations as the language is currently defined. Other languages
(esp. Python) lets you import names anywhere in the code, but Java
doesn't. That's why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top