3. Static Import
• Static Import is a new feature added in java.
• In order to access static members, it is necessary to qualify
references with the class they came from.
• That means in order to access the static members of a class, it is
a must to write the member along with its belonging class
name.
3
4. Static Import
For Example:
class A
{
Public static void main(String a[])
{
Int a=36;
Int val=Math.sqrt(a);
4
5. Static Import
System.out.println(“The square root of”+a”is”+val);
}
}
o/p : The square root of 36 is 6
In order to avoid unnecessary use of static class members like
Math and System, we should Static import.
5
6. Static Import
Import static java.lang.System.out;
Import static java.lang.Math.sqrt;
class A
{
Public static void main(String a[])
{
6
7. Static Import
Int a=36;
Int val=sqrt(a);
System.out.println(“the square root of”+a”is”+val);
}
}
• O/p : the square root of 36 is 6
7
8. Static Import
• Note that ambiguous static import is not allowed.
• That means two different classes referencing the same
functionality is not allowed.
Advantage:
• The advantage of using static import is that readability of the
code increases as it avoids writing of belonging class name
each time. 8
9. Access control
• Access specifiers: visibility modifiers.
– Private
– default
– protected
– public
• these specifiers specify the scope .
9
10. Access control
• Private members can be accessed within the class where they
are declared. They are not allowed outside the class.
• Top level classes can be default and public but not private
• we can use these 4 specifies for all the members of the class
• these specifiers are not allowed for local variables
10