Java Packages
Java Packages
Directory Structure: Package names and directory structures are closely related.
Example:
import java.util.*;
Built-in Packages
User-defined Packages
1. Built-in Packages
java.util: Contains utility classes which implement data structures like Linked
List, Dictionary and support ; for Date / Time operations.
java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc). 6)
User-defined Packages
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the package). Then
create the MyClass inside the directory with the first statement being the package
names.
Example
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;
public class MyClass{
public void getNames(String s)
{
System.out.println(s);
}
}
o.getNames(s);
}
}
Using Static Import
class Geeks {
public static void main(String args[]) {
When two packages contain a class with the same name (e.g., java.util.Date and
java.sql.Date), specify the full package name to avoid conflicts.
import java.util.*;
import java.sql.*;
//And then use Date class, then we will get a compile-time error :
Date today ; //ERROR– java.util.Date or java.sql.Date?
The compiler will not be able to figure out which Date class do we want. This
problem can be solved by using a specific import statement:
import java.util.Date;
import java.sql.*;
If we need both Date classes then, we need to use a full package name every time
we declare a new object of that class.
For Example:
import package_name.ClassOne;
import package_one.ClassTwo;