- 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
- package com.instanceofjava;
- class A{
- public static void main(String [] args){
- int number;
- java.util.Scanner in = new java.util.Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- }
- }
- if we use import no need to mention class name in declaration
- package com.instanceofjava;
- import java.util.Scanner;
- class A{
- public static void main(String [] args){
- int number;
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a number to check even or odd");
- number=in.nextInt();
- }
- }
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:
- package com.instanceofjava;
- class StaticImport{
- public static void main(String [] args){
- System.out.println(Math.PI); //3.141592653589793
- System.out.println(Integer.MAX_VALUE);//2147483647
- System.out.println(Integer.parseInt("123"));//123
- }
- }
Using Static import:
static import in java with example:
- package com.instanceofjava;
- import static java.lang.Integer.*;
- import static java.lang.Math.*;
- class StaticImport{
- public static void main(String [] args){
- System.out.println(PI); //3.141592653589793
- System.out.println(MAX_VALUE);//2147483647
- System.out.println(parseInt("123"));//123
- }
- }
static imports in java 1.5 examples:
Importing Math class:
- package com.instanceofjava;
- import static java.lang.Math.*;
- class StaticImport{
- public static void main(String [] args){
- System.out.println(PI); //3.141592653589793
- double square;
- double d1 = 3.0;
- double d2 = 4.0;
- square = sqrt(pow(d1, 2) + pow(d2, 2));
- System.out.println(square);
- }
- }
Importing System class:
- package com.instanceofjava;
- import static java.lang.System.out;
- class StaticImport{
- public static void main(String [] args){
- out.println("Good morning, " + "java2s");
- out.println("Have a day!");
- }
- }
Importing User defined classes:
- package com.instanceofjava;
- class Colors{
- public static int white = 1;
- public static int black = 2;
- public static int red = 3;
- public static int blue = 4;
- public static int orange = 5;
- public static int grey = 6;
- public static int green =7;
- }
- package com.instanceofjava;
- import static com.instanceofjava.Colors.*;
- class StaticImportDemo{
- public static void main(String [] args){
- System.out.println(white );//1
- System.out.println(blue);//4
- }
- }
Arrays and Collections:
- package com.instanceofjava;
- import static com.instanceofjava.Colors.*;
- class StaticImportDemo{
- public static void main(String [] args){
- int[] array = new int[] {5, 4, 6, 3, 2, 1};
- sort(array);
- for (int i = 0; i < array.length; i++) {
- System.out.print(array[i]+" ");
- }
- ArrayList al= new ArrayList();
- al.add(1);
- al.add(12);
- al.add(3);
- al.add(2);
- sort(al);
- Iterator itr= al.iterator();
- while(itr.hasNext()){
- System.out.printl(itr.next()+" ");
- }
- }
- }
OutPut:
- 1 2 3 4 5 6
- 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
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
No comments