Welcome!

Java IoT Authors: Liz McMillan, Pat Romanski, Elizabeth White, Yeshim Deniz, Zakia Bouachraoui

Related Topics: Java IoT, Industrial IoT, Microservices Expo, IBM Cloud, Weblogic, Linux Containers, Containers Expo Blog, IT SOLUTIONS GUIDE, Eclipse

Java IoT: Article

Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)

Lesson 11 in the Hugely Popular "Java Basics" Series by JDJ Editorial Board Member Yakov Fain

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.

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (5) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
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

IoT & Smart Cities Stories
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the premiere long-term, back-end software development partner for start-ups, SMBs, and Fortune 500 companies. Chetu is headquartered in Plantation, Florida, with thirteen offices throughout the U.S. and abroad.
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 18th International Cloud Expo, which will take place on June 7–9, 2016, at the Javits Center in New York City, NY. DatacenterDynamics is a brand of DCD Group, a global B2B media and publishing company that develops products to help senior professionals in the world's most ICT dependent organizations make risk-based infrastructure and capacity decisions.
DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER gives detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPOalso offers sp...
Headquartered in Plainsboro, NJ, Synametrics Technologies has provided IT professionals and computer systems developers since 1997. Based on the success of their initial product offerings (WinSQL and DeltaCopy), the company continues to create and hone innovative products that help its customers get more from their computer applications, databases and infrastructure. To date, over one million users around the world have chosen Synametrics solutions to help power their accelerated business or per...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time t...