• If we want to use 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.
  • Static imports introduced in Java 5.
  • 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:

static import in java with example:
  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. }

static imports in java 1.5 examples:

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 in java:

  • 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


Java programming interview questions
  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. Super keyword interview questions java 
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu