Static import

K

Karsten Wutzke

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
 
M

Mark

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
 
K

Karsten Wutzke

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
 
K

Karsten Wutzke

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
 
L

Lew

Karsten said:
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 said:
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'.
 
L

Lew

Karsten said:
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'.
 
M

Mike Schilling

Karsten said:
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.
 
L

Lew

Lew said:
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:
 
K

Karsten Wutzke

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
 
K

Karsten Wutzke

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
 
R

Roedy Green

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
 
K

Karsten Wutzke

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
 
K

Karsten Wutzke

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
 
R

Roedy Green

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
 
R

Roedy Green

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
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top