• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Import static

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com.name;

interface xyz {

public static final double d1=10.0;
public static final double d2=11.0;

}
Below given imports are correct??
when i need to implement above interface in concret class of another packege

package B;
import static com.name.xyz.*; or

import static com.name.xyz.d1;
import static com.name.xyz.d2;

class Test implements xyz {
public static void main(String arg[]) {

System.out.println(d1 + " "+d2);
}
}

correct me if i am wrong
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can call the package as,

import static com.name.*;

The above statement imports all static members of package.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by praks bodduna:
u can call the package as,

import static com.name.*;

The above statement imports all static members of package.


Not quite. A package doesn't really have static members, so a static import must specify a type -- not just a package.

As detailed in 7.5 of the JLS, the 4 forms of import statements are:
  • import TypeName;
  • import PackageOrTypeName.*;
  • import static TypeName.Identifier;
  • import static TypeName.*;
  • If you try to use a static import statement like...

    import static packageName.*;

    ...you will get a compiler error of "cannot find symbol" for the packageName because it's looking for a type here.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Manju,

    The static imports are correct -- they will allow you to use variable names d1 and d2 without qualification. In fact, class Test does not need to implement xyz for this.

    If you do implement the interface, then you will need an import statement that allows the compiler to find xyz, which would be either...

    import com.name.xyz;

    ...or...

    import com.name.*;

    (But if your class implements the interface, then it can use d1 and d2 without qualification, so the static import would not be necessary.)
    [ December 14, 2006: Message edited by: marc weber ]
     
    ashni Prakash
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey marc that was a neat explaination.
     
    Manju Devarla
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    if the options are

    import com.name.*; and
    import static com.name.xyz.*;

    which one to select..? (only one option to select)
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Manju Devarla:
    if the options are

    import com.name.*; and
    import static com.name.xyz.*;

    which one to select..? (only one option to select)


    These do different things, so it depends on what you want to do.

    import com.name.*; makes everything in the com.name package visible using simple (unqualified) names. For example, if you wanted to implement the xyz interface without the import statement, you would need to use a qualified name of "com.name.xyz." The import statement opens the namespace, and allows you to simply say "xyz" instead.

    Note that the wildcard * opens the namespace to include everything in the package. So it's not always clear to another reader why the import statement is there, and it can cause conflicts. For example, if com.name also included a class called abc, and your code defined a different class called abc, then there would be a clash of names when you said "abc." To avoid this, it's often better to import only the specific type you need: import com.name.xyz;

    import static com.name.xyz.*; makes all the static members of xyz visible using simple (unqualified) names. For example, if you wanted to use static members of xyz without an import statement, you would need to use a qualified name of "com.name.xyz.d1." Even if you had imported the interface with import com.name.xyz; you would still need to qualify the variable name as xyz.d1 (unless you had already implemented the interface, which would make the variables available). The static import allows you to simply say d1 even without implementing the interface.

    Note that the static import only makes the static members visible -- it does not make the class or interface itself visible. So import static com.name.xyz.*; does not allow you to say "implements xyz" because only the static members of xyz are visible -- not xyz itself. And here again, the wildcard * can cause confusion and/or conflicts.

    So the bottom line is this:
  • If you want to extend the class or implement the interface, then use a non-static import (import com.name.*; or better yet import com.name.xyz;).
  • If you only need to use the static members of the class or interface, then use a static import (import static com.name.xyz.*; or better yet import static com.name.xyz.d1;).

  • [ December 16, 2006: Message edited by: marc weber ]
     
    Ranch Hand
    Posts: 1274
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nice explanation Marc,
    I saved it.


    Only one little tiny thing to add:

    You cannot import interface xyz.

    It is not public and so not visible outside its package...


    Yours,
    Bu.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Burkhard Hassel:
    ...It is not public and so not visible outside its package...


    Good point! I didn't even notice that.
     
    Skool. Stay in. Smartness. Tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic