![]() |
IMPORT STATIC; Why is "import static" file scope? Why not class scope?
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! |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
On 9 Oct 2004 10:00:24 -0700, Paul Opal wrote:
> Why do we write: > > import static BorderLayout.*; > class MyStuff { > ... > } Why would *you* write that? It does not compile. (even with the '...' commented out) -- Andrew Thompson http://www.PhySci.org/codes/ Web & IT Help http://www.PhySci.org/ Open-source software suite http://www.1point1C.org/ Science & Technology http://www.lensescapes.com/ Images that escape the mundane |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
opalpa@gmail.com (Paul Opal) writes:
> 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. |
Re: IMPORT STATIC; Why is "import static" file scope? Why notclass scope?
Andrew Thompson wrote:
> On 9 Oct 2004 10:00:24 -0700, Paul Opal wrote: > > >>Why do we write: >> >>import static BorderLayout.*; >>class MyStuff { >> ... >>} > > > Why would *you* write that? It does not compile. > (even with the '...' commented out) That's because it's so new! Check this out: <http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html> -- Java/J2EE/UNIX consulting and remote development. |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
Sudsy <bitbucket44@hotmail.com> wrote on Sat, 09 Oct 2004 14:17:29
-0400 in comp.lang.java.programmer: >Andrew Thompson wrote: >> On 9 Oct 2004 10:00:24 -0700, Paul Opal wrote: >> >> >>>Why do we write: >>> >>>import static BorderLayout.*; >>>class MyStuff { >>> ... >>>} >> >> >> Why would *you* write that? It does not compile. >> (even with the '...' commented out) > >That's because it's so new! Check this out: ><http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html> 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 |
Re: IMPORT STATIC; Why is "import static" file scope? Why notclass scope?
Dave Glasser wrote:
> Sudsy <bitbucket44@hotmail.com> wrote on Sat, 09 Oct 2004 14:17:29 <snip> > 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... -- Java/J2EE/UNIX consulting and remote development. |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
On Sat, 09 Oct 2004 19:49:42 -0400, Sudsy wrote:
> ...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. And I'm still wonderring how you compile it.. (shrugs) I have 1.5, but javac still whines plaintively at that code sample. -- Andrew Thompson http://www.PhySci.org/codes/ Web & IT Help http://www.PhySci.org/ Open-source software suite http://www.1point1C.org/ Science & Technology http://www.lensescapes.com/ Images that escape the mundane |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
Andrew Thompson <SeeMySites@www.invalid> wrote:
> On Sat, 09 Oct 2004 19:49:42 -0400, Sudsy wrote: > >> ...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. > > 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). -- Oscar Kind http://home.hccnet.nl/okind/ Software Developer for contact information, see website PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2 |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
Dave Glasser wrote:
> 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 |
Re: IMPORT STATIC; Why is "import static" file scope? Why not class scope?
On Sun, 10 Oct 2004 09:14:03 +0200, Oscar kind wrote:
> Andrew Thompson <SeeMySites@www.invalid> wrote: >> On Sat, 09 Oct 2004 19:49:42 -0400, Sudsy wrote: >> >>> ...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. ... >> ...I have 1.5, but javac still whines plaintively >> at that code sample. ... > I expect that a switch "-source 1.5" is needed. Right you are. I had forgotten that detail since I am still concentrating on 1.4 compatible, with some core classes 1.1 compatible. I'm glad that's resolved, it was bugging me. Now ..what was the question? ;) -- Andrew Thompson http://www.PhySci.org/codes/ Web & IT Help http://www.PhySci.org/ Open-source software suite http://www.1point1C.org/ Science & Technology http://www.lensescapes.com/ Images that escape the mundane |
| All times are GMT. The time now is 12:18 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.