Java Package as the name suggest packaging multiple related java files in to one folder and Static import allows us to access method without using the fully qualified name.
There are two type of packages in java:
Below is the example for importing a package inside class:
import java.util.List;
java: java is a top level package.
util: util is sub level and child package of java.
List: List is a class defined in java.
In the above code snippet we have Calculator and TestClass from two different packages. We can access Calculator class inside com.project.core.main using import statement.
Note:
Rules:
a. We can import all the classes using * (asterisks ).e.g.
import com.test.package.*;
b. We can import only required class using below startment:
import com.test.package.class_name_1;
import com.test.package.class_name_2;
2. How we can access, if we have class(TestClass) with the same name in different package.
Import com.project.package.first.*;
Import com.project.package.second.*;
first.TestClass firstTestClass = new first.TestClass();
second.TestClass firstTestClass = new second.TestClass();
Static import allows us to access method without using the fully qualified name.
We can make our statement shorter using static import.
Note: Use static import in case we are using a lot of static members and variables.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved