Monday 16 November 2015

java static import

First let us consider about "java import " statement and how it can work before going to static import. Let us consider the two import statements,



1) import java.mypack.class A
2) import java.mypack.

In the statement(1), we can use the class A without referring the entire package throw out your program directly.For example we can create an object class A as: class A obj = new class A();

But in statement(2), we can use all the classes and methods that contain in that package.

But there is another case, one use the class or method in package without importing package like.

package.class A obj = new package.class A();

  It becomes so tedious or ugly. This situation is also comes under static member also let us consider.

double r = Math.cos(Math.PI*theta); Here the method cos is static method in the class Math. This can be written as

double r = cos(PI*theta);This is the case where static import comes in java. Can be written as

import static java.lang.Math.PI;
import static java.lang.Math.cos;

What is static import ?

                             static import is a keyword introduced in java to access class members like fields and methods directly without referring their class name. The java 5.0 adds this feature to java. Let us see in more details

A static class is a class that can be declared as

static public class A
{

// class members

}

The members of that class can be accessed by using as

class_name.variable;
class_name.method();

This can also be happen even in packages also. For example in java.lang package we have the Math class which is static in nature.If you want to access any of  the methods we can write as

Math.sqrt(), Math.pow()....etc

The most known statement is "System.out.println()" Here the System is static class, so the method "out" can be used as System.out( that is class name.method ).

Import Keyword:-  When your using this keyword in your program it imports all the non static methods into your program but not static methods. So if any one want static class or static members one can use as described above.

                         But this can be eliminated in JDK 5.0 specification by introducing a "static import" features and if one can use the static import instead of import it can works as follows

static import java.lang.System.out;

       Then you can use as
out.println("........................your message here............................");

When using static import, it is possible to refer to static members directly by their name, without having to qualifying them with the name of their class.                       

The following examples show how static import cab be used


1
2
3
4
5
6
7
8
9
import static java.lang.System.*;
import static java.lang.Math.*;
class StaticImportDemo
{
    public static void main(String args[])
    {
        out.println("In println() "+PI);
    }
}

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top