0% found this document useful (0 votes)
1 views3 pages

Java Packages and Access Control

The document explains Java packages, which are namespaces for organizing related classes and interfaces, and provides syntax for defining and compiling them. It also discusses classpath settings for locating user-defined classes and the four access specifiers (private, default, protected, public) that control visibility. Additionally, it covers how to import packages to use classes from other packages, with examples provided for clarity.

Uploaded by

charanmekala17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Java Packages and Access Control

The document explains Java packages, which are namespaces for organizing related classes and interfaces, and provides syntax for defining and compiling them. It also discusses classpath settings for locating user-defined classes and the four access specifiers (private, default, protected, public) that control visibility. Additionally, it covers how to import packages to use classes from other packages, with examples provided for clarity.

Uploaded by

charanmekala17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Packages and Access Control

1. Defining a Package

A package in Java is a namespace that organizes a set of related classes and interfaces.

Syntax to define a package:


package packagename;

Example:
package mypackage;
public class MyClass {
public void show() {
System.out.println("This is my package class.");
}
}

How to compile:
javac -d . MyClass.java
This will create a folder named 'mypackage' with the compiled .class file inside.

Benefits of packages:
- Avoids class name conflicts.
- Easier maintenance.
- Access protection and encapsulation.

2. Classpath

Classpath is the path used by JVM and Java compiler to locate user-defined classes and packages.

You can set it:


- Temporarily using the command line with -cp or -classpath.
- Permanently via environment variables.

Example:
java -cp .;lib/utils.jar MyApp (Windows)
java -cp .:lib/utils.jar MyApp (Linux/Mac)
Java Packages and Access Control

Default classpath is current directory ('.') if not specified.

3. Access Specifiers

Java provides 4 access specifiers that control the visibility of classes and members:

1. private:
- Accessible only within the same class.

2. default (no keyword):


- Accessible only within the same package.

3. protected:
- Accessible within the same package and by subclasses.

4. public:
- Accessible from any class in any package.

Example:
public class A {
private int a;
public int b;
protected int c;
int d; // default
}

4. Importing Packages

To use a class from another package, we use the 'import' statement.

Syntax:
import packagename.ClassName;
import packagename.*; // imports all classes
Java Packages and Access Control

Example:
import java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
}

java.lang package is imported by default in every Java program.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy