Skip to main content

Packages In Java

Java Packages — Notes

Packages

Packages are an encapsulation mechanism that group related classes and interfaces into a single unit.

What is a package?

A package groups related types (classes/interfaces) for modularity, naming, and access control.

Examples

  • java.sql — classes and interfaces related to database operations.
  • java.io — classes and interfaces for file I/O operations.
Packages example

Main advantages

  • Resolve name conflicts (unique identification).
  • Improve modularity of the application.
  • Improve maintainability.
  • Provide security for components (access control).

Naming convention

Use the internet domain name in reverse followed by module/submodule/class. Example:

com.icicibank.loan.housing.Account
  • com.icicibank — reversed domain
  • loan — module
  • housing — submodule
  • Account — class

Code example

package com.code.java; public class Test { public static void main(String[] args) { System.out.println("hello"); } }

Compiling & class file placement

javac Test.java
The generated .class file is placed in the current working directory.

javac example

javac -d

javac -d . Test.java

-d specifies the destination root where the .class files are placed (creates folder structure for the package if needed).

  • If destination doesn't exist you can specify another path: javac -d F: Test.java
  • If specified directory is not present and not creatable: compile error (directory not found).

Execution

Run using fully qualified name:

java com.code.java.Test

Rules & Conclusion

  • At most one package statement is allowed per source file.
  • If used, the package declaration must be the first non-comment statement in the file.
  • Valid Java source file structure (when package is present):
  1. package statement
  2. import statements
  3. class / interface / enum declaration
file structure

Access modifiers (summary)

Recommended: make data members private, expose methods as public when needed.

Context public private protected default
Within same class AllowedAllowedAllowedAllowed
Child class (same package) AllowedNot allowedAllowedAllowed
Non-child class (same package) AllowedNot allowedAllowedAllowed
Child class (outside package) AllowedNot allowedAllowed (use child reference)Not allowed
Non-child class (outside package) AllowedNot allowedNot allowedNot allowed

Access order (least → most permissive): private < default < protected < public

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...