Coding Raptor

Programmers' Nest

You are here: Home / java / core-java / static import in Java

March 18, 2016

static import in Java

A static import allows you to access static members (both methods and instance variables) of a class possibly located in  different package. Much like the import statement discussed earlier, the static import is also a syntactic sugar. By using an import statement you can avoid writing package names before classes and interfaces. Similarly, by using static import you avoiding writing both package and class names before static members. This kind of import was introduced in Java 5 (Tiger).

Examples of static import

The following example demonstrates how to use static import –

static import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.codingraptor.java;
 
import static java.Math.PI;
// by virtue of statement above
// Math.PI can simply be accessed as
// PI
 
public class Circle {
 
  // use PI to calculate area of a circle
  // note that method using static import
  // need not be static itself
  
  public double calculateArea(double radius) {
    // no need to say Math.PI
    return PI * radius * radius;
  }
 
  public double calculatePerimeter(double radius) {
    // no need to say Math.PI
    return PI * 2 * radius;
  }
...
}

  • The first point to note here is that static import is written as
/* a static import template */

import static packagename.ClassName.staticMember;
  • As you can see from the code above,  import is a convenience feature. Even without it we could have calculated the area and perimeter of circle. Just that it would have meant a few extra keystrokes and some extra code to read for the maintainer later on.
  • Both instance and static members of class can use the member that has been statically imported.

Pros and Cons of static import

  1. Reduces the clutter. Allows you to use short names for methods and variables rather than fully qulified names.
  2. Like other imports, it can create readability and maintenance problems. For example, it is much better to say Math.PI rather than simply PI.
    A close realy world analogy would be saying John instead of John F. Kennedy. To most people John is a common noun and John F. Kennedy a household name.
  3. Introduces chances of name conflict. Once you have statically imported Math.PI and start calling it PI, you cannot statically import another member called PI. For example, you may have your own Math class where perhaps the value of π  is more precise. You cannot statically import both these π. But you can import your class and use both π  by saying Math.PI or MyClass.PI .

We are social

Spread the word
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Follow CodingRaptor
Facebooktwittergoogle_plusrssFacebooktwittergoogle_plusrss
Post Views: 38

Share with your friends on other avenues:

  • Twitter
  • Facebook
  • Google
  • Reddit
  • Pinterest
  • Tumblr
  • Print
  • LinkedIn
  • Email
  • More
  • Pocket
  • Skype

Related

Article by P T / core-java, java Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Also Check These Out!

  • Factors in R
  • Basic Data Structures V: Arrays in R
  • Basic Data Structures IV: Lists in R
  • Basic Data Structures III: Data Frames in R
  • Basic Data Structures II: Matrices in R
  • Basic Data Structures I: Vectors in R
  • Atomic or Basic Data Types and Scalars in R
  • Getting Started with R: Packages, Help & Workspace (Part 2)
  • Getting Started with R: Installing and Running R (Part 1)
  • Floating-Point in Java: Representation, Comparison, Equality & A Few Shockers
  • Java Ternary Operator a.k.a Conditional Operator
  • Arrays In JavaScript
  • Coding Raptor Turns 1
  • Java EnumMap with Example
  • LinkedHashMap Example: LRU Cache Implementation
  • LinkedHashMap with Example
  • Java TreeMap with Example
  • Java Collections: Java HashMap Internals
  • Java HashMap with Example
  • Java Map
  • Java Collections: EnumSet with Example
  • Java Collections: Problem Solving with Java Sets
  • Java Collections: Differences between TreeSet, LinkedHashSet and HashSet
  • Java Collections: Sets
  • Using Lists in Java
  • Java Collections: Using Lists
  • Java Collections: Interfaces
  • Java Collections: General Overview
  • Debugging in Netbeans
  • Learn Basics of Core Java in 49 Lessons
  • Varargs in Java
  • Exceptions in Java VIII: Creating Custom Exceptions
  • Exceptions in Java VII: throw and throws in Java
  • Exceptions in Java VI: Types and Hierarchy
  • Exceptions in Java V: try with Resources
  • Exceptions in Java IV: Summary of Rules for try-catch-finally
  • Exceptions in Java iii: Control Flow and Multiple try, catch, finally
  • Exceptions in Java II: try, catch, finally
  • Exceptions in Java I: Introduction
  • Strings in Java VI: More String Methods
  • String in Java V: The intern Method
  • String in Java IV: Important String Methods
  • Strings in Java III: StringBuilder and StringBuffer
  • Strings in Java II: Immutability
  • Strings in Java I: What is a String?
  • Arrays in Java V: Iteration
  • Arrays in Java IV: Manipulating Individual Elements
  • Arrays in Java III: Multidimensional Arrays
  • Arrays in Java II: Declaring, Initializing, Instantiating Arrays
  • Arrays in Java I: What is an Array?
  • Using Environment Variables as Inputs
  • Command Line Arguments in Java
  • static import in Java
  • The import Statement in Java
  • Packages in Java
  • Non Access Modifiers in Java III: abstract, transient, volatile, native
  • Non Access Modifiers in Java II: final, synchronized
  • Non Access Modifiers in Java I: static and strictfp
  • Access Modifiers in Java
  • Looping Construct: for Loop
  • Looping Construct: while and do-while
  • Statements, Expressions and Code Blocks
  • Operators in Java
  • Conditional Statements in Java
  • Reference Data Types
  • Literals in Java
  • Primitive Data Types in Java
  • Variables in Java
  • Compiling and Running Java from Command Line
  • First Java Program: Hello World!
  • Java IDEs and Editors III: Sublime Text and Editplus
  • Java IDEs and Editors II: Eclipse IDE
  • Java IDEs and Editors I: Netbeans
  • Setting up Java Development Environment
  • Java’s Program Execution Model and WORA: Compilation & Interpretation
  • Inside JVM 101: What does JVM do and Memory Areas
  • JVM, JRE, JDK and JIT explained
  • Features of Java and White Paper Buzzwords
  • Beginning Java: History in Brief
Facebooktwittergoogle_plusrssFacebooktwittergoogle_plusrss

Copyright © 2017 · Coding Raptor

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.