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

Discussion in 'Java' started by Paul Opal, Oct 9, 2004.

  1. Paul Opal

    Paul Opal Guest

    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!
    Paul Opal, Oct 9, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Oct 9, 2004
    #2
    1. Advertising

  3. (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.
    Tor Iver Wilhelmsen, Oct 9, 2004
    #3
  4. Paul Opal

    Sudsy Guest

    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.
    Sudsy, Oct 9, 2004
    #4
  5. Paul Opal

    Dave Glasser Guest

    Sudsy <> 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
    Dave Glasser, Oct 9, 2004
    #5
  6. Paul Opal

    Sudsy Guest

    Re: IMPORT STATIC; Why is "import static" file scope? Why notclass scope?

    Dave Glasser wrote:
    > Sudsy <> 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.
    Sudsy, Oct 10, 2004
    #6
  7. 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
    Andrew Thompson, Oct 10, 2004
    #7
  8. Paul Opal

    Oscar kind Guest

    Andrew Thompson <> 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
    Oscar kind, Oct 10, 2004
    #8
  9. Paul Opal

    Chris Uppal Guest

    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
    Chris Uppal, Oct 10, 2004
    #9
  10. On Sun, 10 Oct 2004 09:14:03 +0200, Oscar kind wrote:
    > Andrew Thompson <> 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
    Andrew Thompson, Oct 10, 2004
    #10
  11. Paul Opal

    xarax Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:...
    > 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.


    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.
    xarax, Oct 10, 2004
    #11
  12. Paul Opal

    Chris Uppal Guest

    xarax wrote:

    > 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
    Chris Uppal, Oct 10, 2004
    #12
  13. Paul Opal

    Paul Opal Guest

    Thx.

    Tor Iver Wilhelmsen <> wrote in message news:<>...
    > (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.
    Paul Opal, Oct 11, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew James
    Replies:
    1
    Views:
    963
    Diez B. Roggisch
    Dec 4, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  3. Robin
    Replies:
    0
    Views:
    407
    Robin
    Jun 6, 2007
  4. Hicham Mouline
    Replies:
    5
    Views:
    2,351
    James Kanze
    Dec 19, 2008
  5. Shriramana Sharma
    Replies:
    4
    Views:
    289
    Bart van Ingen Schenau
    May 5, 2013
Loading...

Share This Page