Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Since Enums are static, could I use either static import or regular import on them

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. package com.sun2;
2. public enum Seasons {SUMMER, FALL, WINTER, SPRING}


And:

1. import com.sun2.Seasons;
2. import static com.sun2.Seasons.*;
3. class Enum3a {
4. Seasons s = FALL;
5. }


And:

1. import com.sun2.*;
2. import static com.sun2.Seasons.FALL;
3. class Enum3b {
4. Seasons s = FALL;
5. }

The above classes run fine using regular imports working together with static imports. But since they are just for name resolution, is there a way to use only static imports and have the above 2 classes compile fine? Please answer while remebering Enums are static values any way.
[ June 13, 2006: Message edited by: Firas Zureikat ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to import both because at line Seasons s = FALL;

FALL is static need to import it by static import.

plus

You have to import Seasons class as well.

line import com.sun2.*; will only import all top level classes in the package com.sun2.

Whereas line import static com.sun2.Seasons.*; will only import all static things defined in Seasons class.


regards
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given

package xyz;

public class Test{
public static class Utilities{}
public enum {X,Y,Z};
}


and we want to use ONLY static imports on the following:

class UsingOnlyStaticImports{
public static void main(String...statics){
Utilities u = new Utilities();

for(Inner i: Inner.values())
out.print(i);
}}

To get it running fine(compiles and runs fine). Is this possible?

Yes, use the following:


import static java.lang.System.out;
import static xyz.Test.*;


class UsingOnlyStaticImports{
public static void main(String...statics){
new Utilities;

for(Inner i: Inner.values())
out.print(i);
}}


But how come this also works fine (runs fine) but for my first example you say I must use regular imports for top class? I am sorry I want to know if it's possible to to use the same thing on first example? (I mean only static imports)

I know seasons is a tope level class but still it's static. I mean Enum are by definition static including the constants they contain are also statics...So every thing about them is static. So somehow static imports could be possible for 1st example to import seasons, right?

[ June 13, 2006: Message edited by: Firas Zureikat ]
[ June 13, 2006: Message edited by: Firas Zureikat ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Firas Zureikat
I know seasons is a tope level class but still it's static. I mean Enum are by definition static including the constants they contain are also statics...So every thing about them is static. So somehow static imports could be possible for 1st example to import seasons, right?



Here Seasons is not static.

Only enum defined as a member in a Top level class will be static.

If you define enum as a top level class, then it will not be static.

REMEMBER Top level classes can't be static.
[ June 13, 2006: Message edited by: Naseem Khan ]
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I got it....Top level classes/enums inside packages even if they are public must be imported regularly (not possible with static import because top level enums are never static) before use or use their fully qualified names..


Thanks
[ June 13, 2006: Message edited by: Firas Zureikat ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I compile above code on jdk 1.5.

Then I created back java file from the class which I got using jad.

Have a look on the source code.


[ June 13, 2006: Message edited by: Naseem Khan ]
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting ....Top level enums are just classes and everything else about them has the static modifier...
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very Interesting

That the reason why you need to


  • import FALL by static import
  • Seasons class by normal import

  •  
    Firas Zuriekat
    Ranch Hand
    Posts: 144
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Naseem, I vividly remember a very similar question on todays SCJP exam that I just took

    It's possible to use static import only in case you only want to import a static class member. Then there was another option (the quesyion asked to choose 2 options) where an ordinary import alone can be used for importing the static class member.
    [ June 14, 2006: Message edited by: Firas Zureikat ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic