PROGRAMMING IN JAVA - LECTURE -51- STATIC IMPORTAND USE OF STATIC IMPORT- BCA- S3.

Опубликовано: 30 Сентябрь 2024
на канале: BOSCOCAMPUSVISION
46
2

Static Import
Static import is another language feature introduced with the JZSE 3.0 release. This feature eliminates the need of qualifying a static member with the class name. The static import declaration is similar to that of import, We can use the import statement to import classes from packages and use them without qualifying the package. Similarly, we can use the static import statement to import static members from classes and use them without qualifying the class name. The syntax for using the static import feature is:
import static package-name.subpackage-name.class-name.
staticmember-name;
(or)

import static package-name.subpackage-name.class-name.*;
Advantage of static import:
• Less coding is required if you have access any static member of a class often.
Disadvantage of static import:
• If you overuse the static import feature, it makes the program unreadable and unmaintainable.
import static java.lang.System.out;
import static java.lang.Math.*;
class Demo1{
public static void main(String args[])
{
double var1= sqrt(5.0);
double var2= tan(30);
out.println("Square of 5 is:"+var1);
out.println("Tan of 30 is:"+var2);
}
}
Use of Static import
import static java.lang.Math.*;
public class mathop
{
public void circle(double r)
{}
double area=PI*r* r;
System.out.println(“The Area of Circle is :"+area);
}
public static void main(String args[])
{
Mathop obj=new mathop();
obj.circle(2.3);