Date August-3-2016
1.
Static import:-
In java application, by using “import”
statements we are able to make available classes and interface of a particular
package to the present java file, in this case, we can use classes and
interface directly without using package name every time.
Ex:-
import java.io.*;
-----
-----
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Similarly, to make available all static
members of a particular class or interface to the present java file we have to
use “static” import” feature provided by JDK 5.0 version.
If we import static member of a particular
class or interface to the present java file then we are able to access that
static member directly without using class name and reference variable of the
respective
Syntax 1:-
import static package_Name.class_Name.*;
it able to import all static members from the specified
class
Syntax 2:-
import static package_Name.class_Name.member_Name;
it able to import only the specified member from the
specific class.
Ex code :-
import static java.lang.Thread.*;
import static java.lang.System.out;
class Test
{
public
static void main(String[] args)
{
System.out.println(MIN_PRIORITY);
System.out.println(NORM_PRIORITY);
System.out.println(MAX_PRIORITY);
}
}
Output:-
1
5
10
Static flow of execution/static content
Static content is represented in the form of the following
three elements
1.
Static variables
2.
Static method
3.
Static block
4.
When static variables, static blocks are
recognized and executed at the time of loading the respective class byte code
to the memory, but static method will be recognized and executed at the time of
calling that method.
Ex code 1:-
class A
{
static
int i=m1();
static
int m1()
{
System.out.println("m1-A");
return
10;
}
static
{
System.out.println("SB-A");
}
}
class Test
{
public
static void main(String[] args)
{
A
a=new A();
}
}
Output:-
m1-A
SB-A
Ex code 2:-
class A
{
static
{
System.out.println("SB-A");
}
static
int i=m1();
static
int m1()
{
System.out.println("m1-A");
return
10;
}
}
class Test
{
public
static void main(String[] args)
{
A
a1=new A();
A
a2=new A();
}
}
Output:-
SB-A
m1-A
Ex code 3 :-
class A
{
A()
{
System.out.println("A-Con");
}
static
{
System.out.println("SB-A");
}
int
m1()
{
System.out.println("m1-A");
return
10;
}
static
int i=m2();
{
System.out.println("IB-A");
}
static
int m2()
{
System.out.println("m2-A");
return
20;
}
int
j=m1();
}
class Test
{
public
static void main(String[] args)
{
A
a1=new A();
A
a2=new A();
}
}
Output:-
SB-A
m2-A
IB-A
m1-A
A-Con
IB-A
m1-A
A-Con
Date august-04-2016
No comments:
Post a Comment