• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Static import

July 18, 2012 By j2eereference 3 Comments

static import

static import is one of the new features added in J2SE 5 , which expands the capabilities of the import keyword.

By using static import it is possible to refer to static members directly by their names.We don’t have to qualify them with the class name.This in turn simplifies and shorten the syntax used to refer static member of a class.

Lets understand the use of static import with an example program to find hypotenuse of a right triangle. We know that the formula to find hypotenuse of a right triangle is sqrt(a2+b2), if a and b are the sides of the triangle.

 

Without using static import:

1
2
3
4
5
6
7
8
9
public class StaticImportDemo {
public static void main(String[] args) {
double side1=10;
double side2=12;
double hypotenus;
hypotenus=Math.sqrt(Math.pow(side1, 2)+Math.pow(side2,2));
System.out.println("hypotenus is "+hypotenus);
}
}

By using static import:

1
2
3
4
5
6
7
8
9
10
11
12
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
 
public class StaticImport {
public static void main(String[] args) {
double side1 = 10;
double side2 = 12;
double hypotenus;
hypotenus = sqrt(pow(side1, 2) + pow(side2, 2));
System.out.println("hypotenus is " + hypotenus);
}
}

 Benefits of using static import

  1.  We don’t have to qualify the static method with the class name.
  2.  The code is considerably more readable.

Two different forms of the import static statement.

  1.  import static package.typeName.staticMemeberName

Ex: import static java.lang.math.sqrt;

2.  import static package.typeName.*

Ex: import static java.lang.math.*;
In this case, all the static methods and fields in a class are imported. in our above example , sqrt,pow etc

Another example of static import

1
2
3
4
5
6
7
import static java.lang.System.out;
 
public class OutMethodDemo {
public static void main(String[] args) {
out.println("This is from j2eereference.com");
}
}

output

This is from j2eereference.com

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. dexterous.gal@gmail.com says

    July 20, 2012 at 3:45 pm

    If there are same names and return types for static methods in two different classes and we have done a static import for both then how is it resolved?Is it done based on order of import?

    Reply
    • bs.mohankumar@gmail.com says

      July 20, 2012 at 4:16 pm

      This will give the below compilation error while invoking the method:

      The method method1() is ambiguous for the type Static3

      Please refer the example for more:

      package test;

      public class Static1 {

      public static void method1(){

      System.out.println(“Mohan Static-1”);
      }

      public static void main(String[] args){

      }

      }

      package test;

      public class Static2 {

      public static void method1(){

      System.out.println(“Mohan Static-2”);
      }

      public static void main(String[] args){

      }

      }

      import static test.Static1.method1;
      import static test.Static2.method1;

      public class Static3 {

      public static void main(String[] args){

      method1();

      }

      }

      Hope this has answered 🙂

      Thanks,
      Mohan

      Reply
  2. dexterous.gal@gmail.com says

    July 26, 2012 at 3:20 pm

    Thanx mohan my doubt is cleared.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.