Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

usage and advantage on static imports

 
michael yeung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 5, it will have a new feature "static imports".
What is the usages and advantages on using static imports?

Thanks!!!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true, we should try avoid the bad things, however, people might feel happy for using it.

Nick
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic