| By Yakov Fain | Article Rating: |
|
| April 13, 2005 12:00 AM EDT | Reads: |
116,425 |
Java 5.0 has introduced a new element called static imports. While regular import statements allow you to use the class name without specifying its package name, static imports go further and allow using static members of another class without specifying their class name. Let's use our good old HelloWorld program as an example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Based on this syntax, we can guess that there is a static variable out in the class System that represents some object that in turn has a static method println(). Let's add the static import to eliminate the need of specifying the name of the class System in front of the variable out:
import static java.lang.System.*;
public class HelloWorld {
public static void main(String[] args) {
out.println("Hello World");
}
}
You do not have to use the wildcard (*) and import all static members from a class, but rather specific ones, for example
import static java.lang.System.out;
or for the PI constant from the Math package it can look like this:
import static java.lang.Math.PI;
For example:
import static java.lang.System.out;
import static java.lang.Math.PI;
public class HelloWorld {
public static void main(String[] args) {
out.println("Hello World123");
out.println("Hello PI: " + PI);
}
}
The good news is that if you need to call the println() method multiple times, your program becomes shorter now.
The bad side effect of static imports is that if you'll start overusing this feature, pretty soon your program will actually become less readable. For example, if you'll use static imports of multiple constants from various packages, and someone else (or even yourself five months later) will need to read this program, it may become difficult to understand where all these constants are coming from, especially if you've been using wildcards. The last HelloWorld example gives you an impression that PI constant has been declared somewhere in the HelloWorld class itself. In a small program it's easy to see that this is not the case, but in more complex applications consisting of hundreds of classes this can cause unnecessary confusions.
Prior to Java 5.0 you could have created Java interfaces containing only final static variables, for example
public interface MyConstants {
public static final String companyName="XYZ, Inc.";
public static final String companyAddress="123 Main st.";
}
Now any class that implements MyConstants can use this variables without even mentioning the name of the interface:
class Invoice implements MyConstants{
public static void main(){
System.out.println("Name:"+ companyName);
System.out.println("Address: "+ companyAddress);
}
}
Strictly speaking, if a class implements an interface, you'd expect it to implement some behavior which is not the case in the above example. That's why it's not a recommended way of using interfaces. Oh well, unless you have multiple interfaces with hundreds of constants, I do not see any damage in use of static imports or constant-only interfaces.
Published April 13, 2005 Reads 116,425
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
- Your First Java Program
- Intro to Object-Oriented Programming with Java
- Methods, Constructors, Overloading and Access Levels
- Java Exceptions
- Java Streams Basics
- Reading Data from the Internet
- Java Serialization
- Teaching Kids Programming: Even Younger Kids Can Learn Java
- Java Basics: Introduction to Java Threads, Part 1
- Java Basics: Introduction to Java Threads, Part 2
- SYS-CON Webcast: Eclipse IDE for Students, Useful Eclipse Tips & Tricks
More Stories By Yakov Fain
Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Currently Yakov works on the book for O'Reilly "Enterprise Application Development with Flex". He twits at twitter.com/yfain.
![]() |
Howard N-H 04/13/05 02:53:31 PM EDT | |||
I'm glad you mentioned the use of interfaces as repositories of constants. While it may not be recommended in the strict sense of why interfaces exist, I've used this technique for years on large projects as a good way of organizing and reusing constants. |
||||
![]() |
static imports 04/12/05 06:23:33 AM EDT | |||
So you can use wildcards in the import? Can you use static import for static classes? |
||||
![]() |
Java Basics series 04/12/05 06:12:49 AM EDT | |||
Will the other 10 lessons be on SYS-CON.TV? |
||||
![]() |
Java Live 04/11/05 10:17:40 AM EDT | |||
Is it Yakov Fain who is speaking at the moment? A live Java lesson - cool! |
||||
![]() |
sundeep 04/11/05 06:43:13 AM EDT | |||
I used this lesson in conjunction with the last one, lesson ten on eclipse IDE and it really helped me |
||||
- The Top 150 Players in Cloud Computing
- Commercial vs Federal Cloud Computing
- Why IBM’s Server Chief Got Busted
- Industry Experts Discuss the State of Cloud Computing
- Cloud Expo New York Call for Papers Now Open
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- US Federal Government is Major Cloud Computing Innovator
- Google Wave
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Adaptivity & Cloud Computing: Exclusive Q&A with CEO Tony Bishop
- 4th International Cloud Expo: Photo Album
- The Top 150 Players in Cloud Computing
- SYS-CON.TV: Cloud Computing Expo Power Panel
- Commercial vs Federal Cloud Computing
- Why IBM’s Server Chief Got Busted
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Industry Experts Discuss the State of Cloud Computing
- Deputy CIO of the CIA to Keynote 1st Annual GovIT Expo
- SOA World Power Panel on SYS-CON.TV
- CIA was Headed to an Enterprise Cloud All Along: Jill Tummler Singer
- Cloud Expo New York Call for Papers Now Open
- 1st Annual Government IT Conference & Expo: Themes & Topics
- Stock in Focus: Dragon Capital
- The i-Technology Right Stuff
- Who Are The All-Time Heroes of i-Technology?
- Get the Message
- Where Are RIA Technologies Headed in 2008?
- i-Technology Viewpoint: Is Web 2.0 the Global SOA?
- i-Technology Viewpoint: Thinking Outside the VC Box
- ESB Myth Busters: 10 Enterprise Service Bus Myths Debunked
- i-Technology Viewpoint: When to Leave Your First IT Job
- SOA Web Services Edge Conference Coverage on SYS-CON.TV
- Five Reasons Why Web 2.0 Matters
- SYS-CON.TV's "SOA Web Services" and "Enterprise Open Source" Programs To Air in December
- SOA World Conference & Expo SYS-CON.TV Power Panel Live From Times Square










Cloud computing is a game changer. The cloud ...





















