It's not a secret anymore!*
The moose likes Java in General and the fly likes usage and advantage on static imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "usage and advantage on static imports" Watch "usage and advantage on static imports" New topic
Author

usage and advantage on static imports

michael yeung
Greenhorn

Joined: Oct 20, 2004
Posts: 16
In Java 5, it will have a new feature "static imports".
What is the usages and advantages on using static imports?

Thanks!!!


MCP (70-210), OCP9i DBA, SCJP 1.4 (SAI)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Read this article
http://java.sun.com/developer/JDCTechTips/2004/tt1005.html#1


Groovy
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
What is the usages and advantages on using static imports?

Static import allows you to import static variables from other classes (usually, these variables should be constants).

Thus, it provides a simplifed way for you to access the contants. In the past, you need to use package.class.X to access the constant X which defined in package.class, and if there are 10 constants, you need to refer specify each variable with package.class, which is very trouble.

Now, just as classes, you can import those objects statically. However, it turns out some issues that, if class1 and class2 define the constant with the same name, there will be ambigourity if your class need to access both classes.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
I think, static import feature is not help anythink on coding phase.

(It's make less than of source code) BUT if you use this feature , you have problem when you fixed bug.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
That's true, we should try avoid the bad things, however, people might feel happy for using it.

Nick
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
I never use static import, but the folks at Sun say that it is pretty important for people who program a lot of math. For example,



is easier on the eyes than



Cay


Author of Java 8 for the Really Impatient
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: usage and advantage on static imports
 
Similar Threads
Implemetinng the interface with all static final variables
IMPORTS Doubt
Static imports
static imports
Java 5.0 static import same members collision from different classes