Here we will see how can we import math class in Java.
As we already know to import any class from any package we use the following notation
import packagename.classname;
example
import java.util.Scanner;
Here java is package util is subpackage and Scanner is a class.
By importing the Scanner class we can use all its method in our program.
In Java java.lang is the default package so we don’t require to import this package when using classes of this package like System, Object, String classes.
If you will see java.lang package you will find Math is also available in same package.
So it means we don’t have to import Math class to use in our program.
Let see one example of Math class.
Write a program to find the power of a number
1 2 3 4 5 6 7 8 9 | public class MyClass { public static void main(String args[]) { double a=4.0; double b=5.0; double d=Math.pow(a,b); System.out.println(a+ " Power "+b+"= " + d); } } |
Result
1 | 4.0 Power 5.0= 1024.0 |
Math class definition
In the above program, we can see that we don’t have to import Math class as it is available in java.lang default Java package.
Math class is defined as a final class and it extends Object class.
We can not create a child of the math class as it is final.
Write a program to find the power of a number
1 2 3 4 5 6 7 8 9 | public class MyClass { public static void main(String args[]) { double a=4.0; double b=5.0; double d=Math.pow(a,b); System.out.println(a+ " Power "+b+"= " + d); } } |
Result
1 | 4.0 Power 5.0= 1024.0 |
You can find the detail of the class in the oracle doc
Math class Method Call
All methods are static in Math class so to access methods we used classname.methodname notation.
As above to call power method Math.pow(a,b)
used.
similar for other methods we can call with Math class.
So without importing Math class we solved our problem.
So post title is clickbait?
No No Wait
In Java, there is a concept of static import Let’s discuss it with the Math class.
Math class in Java using static import
In order to access the static members of class, we can use static import.
import static java.lang.Math.PI;
or
import static java.lang.Math.*;
Once a static member is imported that can be used without using class name or object.
like
System.out.println("Value of PI "+PI);
it will show value of PI.
Lets again run our power program with static import;
1 2 3 4 5 6 7 8 9 10 | import static java.lang.Math.*; public class MyClass { public static void main(String args[]) { double a=4.0; double b=5.0; double d=pow(a,b); System.out.println(a+ " Power "+b+"= " + d); } } |
Result
1 | 4.0 Power 5.0= 1024.0 |
In this program, we included static import, and at the function call we not used class name or access specifier
Java Static Import with System class
The most common statement in java is System.out.println().
You can see out is a static member so by using static import we can reduce this statement to out.println().
1 2 3 4 5 6 7 8 9 10 11 | import static java.lang.Math.*; import static java.lang.System.*; public class MyClass { public static void main(String args[]) { double a=4.0; double b=5.0; double d=pow(a,b); out.println(a+ " Power "+b+"= " + d); } } |
It will produce the same result as the above program
When to use static import
When you require to access static member frequently then use static import.
Limits of static import
It will be better to have 1-2 static import in program. More static import leads to unreadable and unmaintainable program code