0% found this document useful (0 votes)
4 views7 pages

Packages in Java

The document explains Java packages, which are collections of related classes and interfaces that help organize code and avoid name conflicts. It details the types of packages (built-in and user-defined), the steps to create and use a package, and the advantages of using packages. Additionally, it introduces enums in Java, providing examples of their usage and integration with switch statements.

Uploaded by

shabnam.mca
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)
4 views7 pages

Packages in Java

The document explains Java packages, which are collections of related classes and interfaces that help organize code and avoid name conflicts. It details the types of packages (built-in and user-defined), the steps to create and use a package, and the advantages of using packages. Additionally, it introduces enums in Java, providing examples of their usage and integration with switch statements.

Uploaded by

shabnam.mca
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

Packages in Java

• Package in Java is a collection of related classes and interfaces that


helps in organizing code, avoiding name conflicts, and improving
reusability.
• Types of Packages
• Built-in packages – predefined (e.g., [Link], [Link]).
Examples:
[Link] → collections (ArrayList, HashMap)
[Link] → input/output classes (File, BufferedReader)
[Link] → database connectivity
• User-defined packages – created by the programmer.
Creating and Using a Package:
• Step 1: Create a package Step 3: Use in another program
import [Link]; // import class from package
package mypackage; // declare package public class Main {
public static void main(String[] args)
public class MyClass { {
MyClass obj = new MyClass();
public void display() { [Link]();
}
[Link]("Hello from }
MyClass in mypackage!");
Project Folder
} │
├── mypackage
} │ └── [Link]

• Step 2: Compile with -d option └── [Link]

javac -d . [Link]
Advantages of Packages
• Organized Code → Easier to maintain large projects.
• Reusability → Classes can be reused in multiple applications.
• Name Conflict Avoidance → Two classes with same name can exist in
different packages.
• Access Control → Provides scope control with access modifiers
(public, protected, default).
• Encapsulation of APIs → Related classes are bundled together.
Example:
• package myPackage; import [Link];

• public class MyClass public class samp


{
•{ public static void main(String args[])
{
• public void getNames(String s) // Initializing the String variable with a value
String s = “Welcome";
• { // Creating an instance of class MyClass in the package.
MyClass o = new MyClass();
• [Link](s);
• } }
[Link](s);

•} }
Example:
• package package_name;
• public class ClassOne import package_name.ClassOne;
import package_one.ClassTwo;
• { public class Testing
• public void methodClassOne() {
public static void main(String[] args)
• { {
• [Link]("Hello there its ClassOne"); ClassTwo a = new ClassTwo();
• } ClassOne b = new ClassOne();
[Link]();
• } [Link]();
package package_one; }
}

public class ClassTwo {


public void methodClassTwo()
{
[Link]("Hello there i am ClassTwo");
}
}
An enum (short for enumeration)
• It is a special data type in Java used to define a collection of named constants.
• Useful when a variable should have only a fixed set of possible values.
enum EnumName
{
CONSTANT1, CONSTANT2, CONSTANT3;
}

enum Day
{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Main {
public static void main(String[] args) {
Day today = [Link];
[Link]("Today is: " + today);
}
}
Enums with Switch
• enum Day
• {
• MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
• }

• class Test
• {
• public static void main(String[] args)
• {
• Day d = [Link];
• switch (d)
{
• case MONDAY:
• [Link]("Start of the week!");
• break;
• case WEDNESDAY:
• [Link]("Midweek!");
• break;
• case FRIDAY:
• [Link]("Weekend ahead!");
• break;
• }
• }
• }

You might also like