The Artima Developer Community
Sponsored Link

Java Buzz Forum
Static import In java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Static import In java Posted: Mar 31, 2015 6:47 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Static import In java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • If we want to reuse any predefined class or user defined class or interface or enum which is present in a package we need to import those  entire packages or those classes so that we can use those classes present inside the package.

  • import packagename.ClassTest;
  • import packagename.*;
  • For example if we want to read some data from keyboard we can use scanner class present in util package.
  • import java.util.Scanner;
  • We can import everything inside a package by using .*
  • import java.util.*;
  • Without importing want to use that class then code seems to like this




  1. package com.instanceofjava;
  2. class A{
  3.   
  4. public static void main(String [] args){
  5.  
  6.    int number;
  7.   java.util.Scanner in = new java.util.Scanner(System.in);
  8.  
  9.     System.out.println("Enter a number to check even or odd");
  10.     number=in.nextInt();
  11.   
  12.  
  13. }
  14. }

  • if we use import no need to mention class name in declaration 


  1. package com.instanceofjava;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class A{
  6.   
  7. public static void main(String [] args){
  8.  
  9.    int number;
  10.   Scanner in = new Scanner(System.in);
  11.  
  12.     System.out.println("Enter a number to check even or odd");
  13.     number=in.nextInt();
  14.   
  15.  
  16. }
  17. }


Static import:

  • Normal imports will import the all the classes so that we can use them. similarly static imports will import all static data so that can use without class name.
  • In JDk 1.5 these static imports introduced.
  • Lets see one program without static import.

 Without Static Import:


  1. package com.instanceofjava;
  2.  
  3. class StaticImport{
  4.   
  5. public static void main(String [] args){
  6.  
  7.     System.out.println(Math.PI); //3.141592653589793
  8.     System.out.println(Integer.MAX_VALUE);//2147483647
  9.     System.out.println(Integer.parseInt("123"));//123
  10.  
  11. }
  12. }

Using Static import:


  1. package com.instanceofjava;
  2.  
  3.  import static java.lang.Integer.*;
  4.  import static java.lang.Math.*;
  5.  
  6. class StaticImport{
  7.   
  8. public static void main(String [] args){
  9.  
  10.     System.out.println(PI); //3.141592653589793
  11.     System.out.println(MAX_VALUE);//2147483647
  12.     System.out.println(parseInt("123"));//123
  13.  
  14. }
  15. }


Importing Math class:

 

  1. package com.instanceofjava;
  2.  
  3.  import static java.lang.Math.*;
  4.  
  5. class StaticImport{
  6.   
  7. public static void main(String [] args){
  8.  
  9.     System.out.println(PI); //3.141592653589793

  10.     double square;
  11.  
  12.     double d1 = 3.0;
  13.     double   d2 = 4.0;
  14.  
  15.     square = sqrt(pow(d1, 2) + pow(d2, 2));
  16.     System.out.println(square);
  17.  
  18. }
  19. }

Importing System class:

 

  1. package com.instanceofjava;
  2.  
  3.  import static java.lang.System.out;
  4.  
  5. class StaticImport{
  6.   
  7. public static void main(String [] args){
  8.  
  9.      out.println("Good morning, " + "java2s");
  10.      out.println("Have a day!");
  11.  
  12. }
  13. }

Importing User defined classes:



  1. package com.instanceofjava;

  2.  
  3. class Colors{
  4.  
  5.      public static int white = 1;
  6.      public static int black = 2;
  7.      public static int red = 3;
  8.      public static int blue = 4;
  9.      public static int orange = 5;
  10.      public static int grey = 6;
  11.      public static int green =7;
  12.  
  13. }


  1. package com.instanceofjava;

  2.  import static com.instanceofjava.Colors.*; 

  3. class StaticImportDemo{
  4.  
  5. public static void main(String [] args){
  6.  
  7.     System.out.println(white );//1
  8.     System.out.println(blue);//4
  9.  
  10. }
  11.  
  12. }


Arrays and Collections:





  1. package com.instanceofjava;

  2.  import static com.instanceofjava.Colors.*; 

  3. class StaticImportDemo{
  4.  
  5. public static void main(String [] args){
  6.  
  7.    int[] array = new int[] {5, 4, 6, 3, 2, 1};
  8.  
  9.         sort(array);
  10.  
  11.      for (int i = 0; i < array.length; i++) {
  12.             System.out.print(array[i]+" ");
  13.       }  
  14.  
  15.   ArrayList al= new ArrayList();
  16.        al.add(1);
  17.         al.add(12);
  18.         al.add(3);
  19.        al.add(2);
  20.  
  21.        sort(al); 

  22.          Iterator itr= al.iterator();
  23.          while(itr.hasNext()){
  24.              System.out.printl(itr.next()+" ");
  25.          }
  26.  
  27. }
  28.  
  29. }

OutPut:

  1. 1 2 3 4 5 6
  2. 1 2 3 12

Advantages and Disadvantages of Static imports

  • One of the advantage of using static imports is reducing keystrokes and re usability.
  • System.out.println() ; we can write as out.println() .  But using eclipse short cut syso (ctrl+sapce)  gives System.out.println() faster than static imports usage. 
  • And there may be a chance of  complexity in readability.
  • If we use class name before method like Math.sqrt() then can understand easily that method belongs to particular class . with static imports reduces readability.
  • One more disadvantage is naming conflicts.
  • If we use Integer.Max_value we cannot use Float.Max_value

Read: Static import In java

Topic: Going Clear Previous Topic   Next Topic Topic: Java Performance News March 2015

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use