Static import

Discussion in 'Java' started by Karsten Wutzke, Jun 26, 2009.

  1. Hello,

    I just compiled a class with a *static* import

    import static org.domain.action.*;

    but I don't get what this means. I had expected an error.

    So, why does this compile? This would mean package-level constants... -
    > ?


    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #1
    1. Advertising

  2. Karsten Wutzke

    Mark Guest

    On 26 June, 11:58, Karsten Wutzke <> wrote:
    > Hello,
    >
    > I just compiled a class with a *static* import
    >
    > import static org.domain.action.*;
    >
    > but I don't get what this means. I had expected an error.
    >
    > So, why does this compile? This would mean package-level constants... -
    >
    > > ?

    >
    > Karsten


    It is probably parsing it as: import all constants from a class called
    "action" in the org.domain package. Remember that that using title
    case for class names is a just a convention that the compiler does not
    enforce. However my development environment is ful of style-check
    alarms so I can't reproduce this!

    Mark
     
    Mark, Jun 26, 2009
    #2
    1. Advertising

  3. On 26 Jun., 13:20, Mark <> wrote:
    > On 26 June, 11:58, Karsten Wutzke <> wrote:
    >
    > > Hello,

    >
    > > I just compiled a class with a *static* import

    >
    > > import static org.domain.action.*;

    >
    > > but I don't get what this means. I had expected an error.

    >
    > > So, why does this compile? This would mean package-level constants... -

    >
    > > > ?

    >
    > > Karsten

    >
    > It is probably parsing it as: import all constants from a class called
    > "action" in the org.domain package. Remember that that using title
    > case for class names is a just a convention that the compiler does not
    > enforce. However my development environment is ful of style-check
    > alarms so I can't reproduce this!
    >
    > Mark


    I never start class names in lowercase! There's no reason to assume I
    do so. "action" is a package, as hinted by the sentence "This would
    mean package-level constants... -> ?"

    So why does it work? It would have been a great mechanism to include
    all package's class constants, but I doubt the Java people would allow
    such a thing.

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #3
  4. On 26 Jun., 15:45, "Scott A. Hightower" <>
    wrote:
    > "Karsten Wutzke" <> wrote in message
    >
    > news:...
    >
    > > Hello,

    >
    > > I just compiled a class with a *static* import

    >
    > > import static org.domain.action.*;

    >
    > > but I don't get what this means. I had expected an error.

    >
    > > So, why does this compile? This would mean package-level constants... -
    > >> ?

    >
    > > Karsten

    >
    > See 7.5 Import Declarations in the Language Reference (Third Edition).  A
    > static import makes available just the static members.  This includes
    > fields, methods and nested classes and interfaces.  The fields are constants
    > only if they are also final.
    >
    > So, even if the most common need is for constants, all of the others are
    > there.
    >
    > You can be explicit and import just one static member or you can import on
    > demand any and all:
    >
    > import static TypeName.Identifier;
    > import static TypeName.*;
    >
    > where "TypeName" is the canonical name of a class or interface type.


    That was *not* my question. Again, my question was, why

    import static org.domain.action.*;

    does compile when action is a package (which it is). This syntax hints
    to me that there are package-level constants... It's not an absolute
    beginner's question that I have.

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #4
  5. Karsten Wutzke

    Lew Guest

    Karsten Wutzke wrote:
    >>> I just compiled a class with a *static* import
    >>> import static org.domain.action.*;
    >>> but I don't get what this means. I had expected an error.
    >>> So, why does this compile? This would mean package-level constants... -
    >>>> ?


    Mark wrote:
    >> It is probably parsing it as: import all constants [sic] from a class called
    >> "action" in the org.domain package. Remember that that using title
    >> case for class names is a just a convention that the compiler does not
    >> enforce. However my development environment is ful of style-check
    >> alarms so I can't reproduce this!


    Karsten Wutzke wrote:
    > I never start class names in lowercase! There's no reason to assume I do so.


    There's no way that the compiler can know that! There's every reason for the
    compiler to assume you do so - you told it that you did by doing a static import.

    > "action" is a package, as hinted by the sentence "This would
    > mean package-level constants... -> ?"


    The compiler doesn't take hints or read Usenet.

    > So why does it work? It would have been a great mechanism to include
    > all package's class constants, but I doubt the Java people would allow
    > such a thing.


    Read Mark's answer again.

    The compiler is unaware and uncaring of whether you ever start class names
    with a lower-case letter. It only cares and reacts to Java syntax, which
    permits lower-case class names.

    The syntax you show is perfectly legal, and tells the compiler that you're
    importing all static elements from a class named 'org.domain.action'.

    --
    Lew
     
    Lew, Jun 26, 2009
    #5
  6. Karsten Wutzke

    Lew Guest

    Karsten Wutzke wrote:
    > That was *not* my question. Again, my question was, why
    >
    > import static org.domain.action.*;
    >
    > does compile when action is a package (which it is). This syntax hints
    > to me that there are package-level constants... It's not an absolute
    > beginner's question that I have.


    Yes, it is. The compiler was trusting your declaration that you are importing
    static members of a class named 'org.domain.action'.

    --
    Lew
     
    Lew, Jun 26, 2009
    #6
  7. Karsten Wutzke wrote:
    > Hello,
    >
    > I just compiled a class with a *static* import
    >
    > import static org.domain.action.*;
    >
    > but I don't get what this means. I had expected an error.
    >
    > So, why does this compile? This would mean package-level
    > constants...


    I presume it means "import the static members of all classes in the
    package org.domain.action". Yuck.
     
    Mike Schilling, Jun 26, 2009
    #7
  8. Karsten Wutzke

    Lew Guest

    Lew wrote:
    > Karsten Wutzke wrote:
    >> That was *not* my question. Again, my question was, why
    >>
    >> import static org.domain.action.*;
    >>
    >> does compile when action is a package (which it is). This syntax hints
    >> to me that there are package-level constants... It's not an absolute
    >> beginner's question that I have.

    >
    > Yes, it is. The compiler was trusting your declaration that you are
    > importing static members of a class named 'org.domain.action'.


    I take that back. On further research I see that should have raised an error.
    JLS 7.5.4:
    > It is a compile-time error for a static-import-on-demand declaration to name
    > a type that does not exist or a type that is not accessible.


    --
    Lew
     
    Lew, Jun 26, 2009
    #8
  9. On 26 Jun., 16:06, Patricia Shanahan <> wrote:
    > Karsten Wutzke wrote:
    > > Hello,

    >
    > > I just compiled a class with a *static* import

    >
    > > import static org.domain.action.*;

    >
    > > but I don't get what this means. I had expected an error.

    >
    > > So, why does this compile? This would mean package-level constants... -
    > >> ?

    >
    > I tried this in Eclipse: import static java.util.*;
    >
    > and got a reasonable error message "Only a type can be imported,
    > java.util resolves to a package". What compiler are you using?
    >
    > Patricia


    Don't know really. I'll have to check. I'm using the all-new Eclipse
    3.5 inbuilt compiler.

    I just realized the imports you tried don't compile for me either. I
    seem to have tried something special:

    The preamble for my class is

    --

    package com.kawoolutions.commons.action;

    import java.util.*;

    import static com.kawoolutions.commons.action.*; //same package as
    above, no error
    //import static com.kawoolutions.commons.action.CommonActions.*;
    //import static com.kawoolutions.commons.action.CommonPrefixes.*;
    //import static com.kawoolutions.commons.action.CommonSuffixes.*;

    public class
    IWantToUseConstantsFromClassesWithinThisPackageWithoutQualifying
    {

    ....

    }

    --

    So the special situation here seems to be that I was trying to do such
    a static import from the same package to avoid the multiple imports
    just for the specific classes. I was just trying it to save a few
    lines to be honest. But why does this work? This should probably be an
    error nonetheless, right?

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #9
  10. Karsten Wutzke

    Roedy Green Guest

    On Fri, 26 Jun 2009 03:58:34 -0700 (PDT), Karsten Wutzke
    <> wrote, quoted or indirectly quoted someone who said :

    >
    >I just compiled a class with a *static* import
    >
    >import static org.domain.action.*;
    >
    >but I don't get what this means. I had expected an error.
    >
    >So, why does this compile? This would mean package-level constants... -


    see http://mindprod.com/jgloss/import.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Deer hunting would be fine sport, if only the deer had guns."
    ~ William S. Gilbert of Gilbert and Sullivan
     
    Roedy Green, Jun 26, 2009
    #10
  11. On 26 Jun., 18:40, Roedy Green <>
    wrote:
    > On Fri, 26 Jun 2009 03:58:34 -0700 (PDT), Karsten Wutzke
    > <> wrote, quoted or indirectly quoted someone who said :
    >
    >
    >
    > >I just compiled a class with a *static* import

    >
    > >import static org.domain.action.*;

    >
    > >but I don't get what this means. I had expected an error.

    >
    > >So, why does this compile? This would mean package-level constants... -

    >
    > see http://mindprod.com/jgloss/import.html


    You didn't read the whole thing right?

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #11
  12. On 26 Jun., 16:04, "Mike Schilling" <>
    wrote:
    > Karsten Wutzke wrote:
    > > Hello,

    >
    > > I just compiled a class with a *static* import

    >
    > > import static org.domain.action.*;

    >
    > > but I don't get what this means. I had expected an error.

    >
    > > So, why does this compile? This would mean package-level
    > > constants...

    >
    > I presume it means "import the static members of all classes in the
    > package org.domain.action".  Yuck.


    Right, that's what I was trying. I admit it would have been strange if
    it had worked.

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #12
  13. Karsten Wutzke

    Roedy Green Guest

    On Fri, 26 Jun 2009 10:35:09 -0700 (PDT), Karsten Wutzke
    <> wrote, quoted or indirectly quoted someone who said :

    >> see http://mindprod.com/jgloss/import.html

    >
    >You didn't read the whole thing right?


    No, but then you did not read all of my response either. I saw you
    were unclear on import static, so I pointed you to material to fill in
    any gaps in your understanding of import and import static. I don't
    know exactly what you are missing, so I offered the whole ball of wax.
    My goal is to help you come fully up to speed, not to solve some
    specific problem for you and spoon feed you the solution. I want to
    leave you a fully competent programmer in the matter of import/import
    static.

    Try again. This time follow the import static link at the bottom of
    the page, and pay particular attention to the gotcha marked with the
    winding road symbol.

    See also:

    See http://mindprod.com/jgloss/newsgroups.html#GIFTHORSE
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Deer hunting would be fine sport, if only the deer had guns."
    ~ William S. Gilbert of Gilbert and Sullivan
     
    Roedy Green, Jun 26, 2009
    #13
  14. On 26 Jun., 21:26, Roedy Green <>
    wrote:
    > On Fri, 26 Jun 2009 10:35:09 -0700 (PDT), Karsten Wutzke
    > <> wrote, quoted or indirectly quoted someone who said :
    >
    > >> seehttp://mindprod.com/jgloss/import.html

    >
    > >You didn't read the whole thing right?

    >
    > No, but then you did not read all of my response either. I saw you
    > were unclear on import static, so I pointed you to material to fill in
    > any gaps in your understanding of import and import static. I don't
    > know exactly what you are missing, so I offered the whole ball of wax.
    > My goal is to help you come fully up to speed, not to solve some
    > specific problem for you and spoon feed you the solution.  I want to
    > leave you a fully competent programmer in the matter of import/import
    > static.
    >
    > Try again. This time follow the import static link at the bottom of
    > the page, and pay particular attention to the gotcha marked with the
    > winding road symbol.
    >
    > See also:
    >
    > Seehttp://mindprod.com/jgloss/newsgroups.html#GIFTHORSE
    > --
    > Roedy Green Canadian Mind Productshttp://mindprod.com
    >
    > "Deer hunting would be fine sport, if only the deer had guns."
    > ~ William S. Gilbert of Gilbert and Sullivan


    I didn't follow the import static link because I found myself at a
    regular "tutorial" page. I just found the static import link and if
    you hadn't mentioned it, I would never have scrolled down to the
    gotcha. Maybe posting the static import link had done the trick,
    maybe. Anyway, I found what you were pointing me to: "I will not let
    you access all the classes in a package with a wildcard syntax."

    BTW there is one typo in the second sentence and the fact static won't
    only allow methods but constants, too. At that point though, the
    basics should be clear, so deleting the first sentence would probably
    be appropriate.

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #14
  15. On 26 Jun., 21:26, Roedy Green <>
    wrote:
    > On Fri, 26 Jun 2009 10:35:09 -0700 (PDT), Karsten Wutzke
    > <> wrote, quoted or indirectly quoted someone who said :
    >
    > >> seehttp://mindprod.com/jgloss/import.html

    >
    > >You didn't read the whole thing right?

    >
    > No, but then you did not read all of my response either. I saw you
    > were unclear on import static, so I pointed you to material to fill in
    > any gaps in your understanding of import and import static. I don't
    > know exactly what you are missing, so I offered the whole ball of wax.
    > My goal is to help you come fully up to speed, not to solve some
    > specific problem for you and spoon feed you the solution.  I want to
    > leave you a fully competent programmer in the matter of import/import
    > static.
    >
    > Try again. This time follow the import static link at the bottom of
    > the page, and pay particular attention to the gotcha marked with the
    > winding road symbol.
    >
    > See also:
    >
    > Seehttp://mindprod.com/jgloss/newsgroups.html#GIFTHORSE
    > --
    > Roedy Green Canadian Mind Productshttp://mindprod.com
    >
    > "Deer hunting would be fine sport, if only the deer had guns."
    > ~ William S. Gilbert of Gilbert and Sullivan


    Epilogue:

    Because of the special situation I was in, doing an import static
    lala.lele.lili.*; on the same package as the class I was in doesn't
    raise a compiler error, so people surprised by that (like me) are
    confused thinking package level constants or all classes' constants or
    methods could be accessible, which is false.

    Karsten
     
    Karsten Wutzke, Jun 26, 2009
    #15
  16. Karsten Wutzke

    Roedy Green Guest

    On Fri, 26 Jun 2009 15:12:50 -0700 (PDT), Karsten Wutzke
    <> wrote, quoted or indirectly quoted someone who said :

    >
    >BTW there is one typo in the second sentence and the fact static won't
    >only allow methods but constants, too. At that point though, the
    >basics should be clear, so deleting the first sentence would probably
    >be appropriate.


    I gave you the information you needed PLUS some review material. If
    you don't understand one aspect of a feature, you are likely missing
    others too. The fact you did not receive my message was because you
    expected to be handed exactly what you wanted no more no less on a
    plate. What you want is not necessarily what you need. Further, the
    answers are NOT for you alone, but for all the novices tracking the
    thread. Most certainly some of them need the extra material. The
    purpose of newsgroups is to share information with everyone. It is not
    just a help desk.

    I started out with a strong prejudice in your favour based on
    experience with other Karstens I have encountered in past. But, now I
    see you suffering from entitlement syndrome.

    http://www.amazon.com/gp/product/04...mp=1789&creative=9325&creativeASIN=0452272041

    I tried to do you a favour and you figuratively spat in my face. I am
    not as likely to leap to your aid in future if I notice it is you who
    is asking.

    It is not your fault. It is difficult to change. But it is going to
    cause you all manner of difficulty until you do. See that book I
    recommended at Amazon. Also see
    http://mindprod.com/jgloss/newsgroups.html See how much of that essay
    explains some of the problems you have had in past.

    I can imagine you going into a library, asking the librarian for a
    book recommendation, then angrily batting her over the head with it,
    shouting, "How DARE you waste MY time. This book told me something I
    already knew!!!!"

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Deer hunting would be fine sport, if only the deer had guns."
    ~ William S. Gilbert of Gilbert and Sullivan
     
    Roedy Green, Jun 29, 2009
    #16
  17. Karsten Wutzke

    Roedy Green Guest

    On Fri, 26 Jun 2009 15:17:14 -0700 (PDT), Karsten Wutzke
    <> wrote, quoted or indirectly quoted someone who said :

    >Because of the special situation I was in, doing an import static
    >lala.lele.lili.*; on the same package as the class I was in doesn't
    >raise a compiler error, so people surprised by that (like me) are
    >confused thinking package level constants or all classes' constants or
    >methods could be accessible, which is false.


    If you made that error, why is it so wicked for others to imagine you
    missed other aspects of package/import too?

    I spent hours writing that essay for you and others like you. Why
    should you not have to spend even a few seconds skimming it for the
    gotcha?

    I website is a bit like book. Do you berate someone who points you to
    a page in Knuth because there is other information on the page not
    relevant to your immediate needs?

    The sun does not rise and set in your armpit.

    You did not hire me to run a help desk for you. I am not obligated to
    customise my answers to your precise specifications. I am not
    obligated to give you ANYTHING. I am not your mother. And even if I
    were, I don't believe in spoon feeding anyone but infants.

    I prepare materials for you and people like you for many reasons:

    1. I like it when I see people increasing their skill rapidly with my
    overview summary introductions, getting the general lay of the land
    before leaping into the details.

    2. I love it when I see people in countries all over the world sharing
    information and co-operating to help increase the general skill level.

    3. I like it when people tell me how the glossary helped them with a
    project.

    4. I like explaining things. It gives me an excuse to tinker about to
    find out how things work and document them.

    5. When I make errors, they are public, and I am more likely to find
    out about hem.

    6. The newsgroups are a fresh source of puzzles to play with and
    document.


    Note that on this list is missing "I always wanted to run a help
    desk". I have done that quite sufficiently for a lifetime.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Deer hunting would be fine sport, if only the deer had guns."
    ~ William S. Gilbert of Gilbert and Sullivan
     
    Roedy Green, Jun 29, 2009
    #17
    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. Paul Opal
    Replies:
    12
    Views:
    974
    Paul Opal
    Oct 11, 2004
  2. JPractitioner
    Replies:
    13
    Views:
    20,194
    Roedy Green
    Feb 24, 2006
  3. Vitali Gontsharuk
    Replies:
    2
    Views:
    579
    Vitali Gontsharuk
    Aug 25, 2005
  4. Artur M. Piwko

    import/from import question

    Artur M. Piwko, Jun 29, 2003, in forum: Python
    Replies:
    1
    Views:
    863
    Peter Hansen
    Jul 2, 2003
  5. Dennis
    Replies:
    2
    Views:
    512
    Dennis
    Aug 18, 2003
Loading...

Share This Page