Summary — Package Statements
1. Packages can be used
a) To group:
- Classes
- Interfaces
- Enums
- Annotations
b) To prevent naming conflicts.
c) To specify access scope.
2. Package Declaration Rules
- Only one package declaration statement can be defined in a source file.
- Package declaration statement must be the first statement in the source file.
3. Source File Rules
- One source file can contain multiple classes.
- All classes available in the source file will be placed in the same package.
4. Public Class Rules
One source file can contain:
- Only one public class
- Many non-public classes
5. File Naming Rule
- When a class is public, the source file name must be the same as the public class name.
6. Package and Public Class Relationship
- One source file can have:
- Only one package declaration statement
- Only one public class
- One package can contain multiple public classes.
7. Multiple Public Classes
- If you want multiple public classes in the same package, place each public class in a separate source file.
Hello.java
package com.javaworldindia.p1;
public class Hello {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Lab.java
package com.javaworldindia.p1;
public class Lab {
public static void main(String str[]) {
System.out.println("Hello Guys!");
}
}
Package Structure Overview
Important Notes
| Rule | Description |
|---|---|
| Package statement | Must be first statement |
| Public class | File name must match class name |
| One file | Only one public class allowed |
| Multiple classes | Allowed in same source file |
| Multiple public classes | Must be in separate files |
| Package purpose | Grouping and avoiding naming conflicts |
IMPORT STATEMENTS
9.
One source file can have zero or more import statements.
10.
Import statement should be available after the package declaration statement and before the class definitions.
11.
When you import a package, then classes available in that package will not be loaded immediately.
Classes will be loaded only when you use the members of the class.
12.
When you are importing multiple package s which are having same class, then don't use multi-type import statements because that may cause ambiguity.
13.
You can't import the same class two times using SINGLE TYPE IMPORT.
14.
Package name should not start with following words:
javaconcom1tocom9
15.
When you are importing multiple package s that contain common classes, then reference to that common class will be ambiguous in your program.
To resolve that ambiguity, you can refer that class with fully qualified class name.
STATIC IMPORTS
16.
Only public static members of a class will be accessed using static imports.
17.
Non-static members of a class will not be accessed using static imports.
18.
Normal imports allow you to access classes whereas static imports allow you to access static members.
So when you use static imports, classes will not be accessed.
19.
When you refer any static member which is available in the same class as well as imported by static imports, then priority will be given to same class member.
20.
When you are using multiple static imports that contain common static members, then reference to those common static members will be ambiguous in your program.
To resolve that ambiguity, you can refer that static member with class name.
21.
When you refer any class which is defined in the same package as well as in imported package, then priority will be given to same package class.
You can use fully qualified class name to access specified class available in imported package.
JAR FILE
22.
When any problem occurs in manifest file while creating executable JAR file, then:
java.io.IOException: invalid header
error will be given at runtime.
23.
Make sure that you have pressed ENTER after class name in the manifest file, otherwise while executing you may get:
no main manifest attribute
error.