JAVA PACKAGE AND MODIFIERS
LESSON 13
INTRODUCTION TO JAVA PACKAGES
• Java packages are containers for organizing related classes and interfaces
• Help avoid naming conflicts and control access
• Similar to directories on a computer
• Examples of built-in packages: [Link], [Link], [Link]
WHAT IS A JAVA PACKAGE?
• Collection of related classes and interfaces
• Stored in a folder structure
• Provides a namespace for the classes it contains
• Helps in organizing large-scale applications
• Allows for better code management and reusability
CREATING A JAVA PACKAGE
• Use the 'package' keyword at the beginning of your Java file
Example:
package [Link];
• Package names are typically in lowercase
• Use reverse domain name convention (e.g., [Link])
USING JAVA PACKAGES
• Import classes from packages using the 'import' keyword
• Two ways to import:
• Import a single class:
import [Link];
• Import all classes from a package:
import [Link].*;
EXAMPLE: CREATING A PACKAGE
File: [Link]
[Link];
public class MyMath {
public static int add(int a, int b) {
return a + b;
}
}
EXAMPLE: USING A PACKAGE
File: [Link]
import [Link];
public class Main {
public static void main(String[] args) {
int result = [Link](5, 3);
[Link]("The sum is: " + result);
}
}
JAVA CLASS MODIFIERS
• Define access levels and behavior of classes
• Four main access modifiers:
• public: Accessible from any other class
• protected: Accessible within its package and by subclasses
• private: Accessible only within its own class
• default (no modifier): Accessible only within its package
JAVA METHOD MODIFIERS
• Same access modifiers as classes (public, protected, private, default)
• Additional modifiers:
• static: Method belongs to the class, not an instance
• final: Method cannot be overridden in subclasses
• abstract: Method has no body, must be implemented by subclasses
EXAMPLE: CLASS WITH DIFFERENT METHOD MODIFIERS
public class Calculator {
public int add(int a, int b) {
return a + b;
}
private int subtract(int a, int b) {
return a - b;
}
protected int multiply(int a, int b) {
return a * b;
}
public static int divide(int a, int b) {
return a / b;
}
}
USING CLASS AND METHOD MODIFIERS
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Addition: " + [Link](10, 5));//
[Link](10, 5); // Error: private method
[Link]("Multiplication: " + [Link](10, 5));
[Link]("Division: " + [Link](10, 5));
}
}
BENEFITS OF JAVA PACKAGES
• Organize related classes and interfaces
• Prevent naming conflicts
• Control access to classes and members
• Improve code readability and maintainability
• Facilitate code reuse across projects
PACKAGE NAMING CONVENTIONS
• Use lowercase letters
• Use reverse domain name (e.g., [Link])
• Avoid using Java keywords
• Use descriptive names (e.g., util, math, io)
• Separate words with underscores if necessary
IMPORTING CLASSES FROM PACKAGES
• Use fully qualified names without import:
[Link] list = new [Link]();
• Import specific classes:
import [Link];
ArrayList list = new ArrayList();
• Import all classes from a package:
import [Link].*;
ArrayList list = new ArrayList();
DEFAULT PACKAGE
• Classes without a package declaration
• Not recommended for production code
• Limited visibility and potential naming conflicts
• Cannot be imported by classes in other packages
• Suitable only for small, temporary applications
STATIC IMPORT
• Allows importing static members of a class
• Syntax:
import static [Link].*;
• Usage:
double radius = 5.0;
double area = PI * radius * radius;
• Use sparingly to avoid confusion
PACKAGE AND CLASS VISIBILITY
• public: Accessible from anywhere
• protected: Accessible within package and subclasses
• default (package-private): Accessible only within package
• private: Accessible only within the same class
Example:
package [Link];
public class MyClass {
public int publicVar;
protected int protectedVar;
int defaultVar;
private int privateVar;
}
BEST PRACTICES FOR USING PACKAGES
• Group related classes and interfaces
• Use meaningful package names
• Avoid cyclic dependencies between packages
• Keep the package structure shallow
• Use subpackages for complex projects
• Document package contents and purpose
COMMON JAVA PACKAGES
• [Link]: Fundamental classes (automatically imported)
• [Link]: Utility classes, collections framework
• [Link]: Input/output operations
• [Link]: Networking operations
• [Link] and [Link]: GUI components
• [Link]: Database operations
SUMMARY
• Java packages organize related classes and interfaces
• Packages help avoid naming conflicts and control access
• Use 'package' keyword to create packages
• Use 'import' to use classes from other packages
• Class and method modifiers control access and behavior
• Follow naming conventions and best practices
• Understand package visibility for effective code organization