Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

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)

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.


IoT & Smart Cities Stories
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Early Bird Registration Discount Expires on August 31, 2018 Conference Registration Link ▸ HERE. Pick from all 200 sessions in all 10 tracks, plus 22 Keynotes & General Sessions! Lunch is served two days. EXPIRES AUGUST 31, 2018. Ticket prices: ($1,295-Aug 31) ($1,495-Oct 31) ($1,995-Nov 12) ($2,500-Walk-in)
According to Forrester Research, every business will become either a digital predator or digital prey by 2020. To avoid demise, organizations must rapidly create new sources of value in their end-to-end customer experiences. True digital predators also must break down information and process silos and extend digital transformation initiatives to empower employees with the digital resources needed to win, serve, and retain customers.
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
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.