Java – How to use Packages to organize code ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

Problem Statement

You need to use packages in java to organize your code.

Solution

Your Java program might the following
Classes
Interfaces
Enums
Other types

There are times when your program might grow larger including th number of java classes used. You might want to organize these source files to that it is easier to maintain and avoid other issues like class name conflicts.

Inorder to organize the code for the application (Eg: websites) , you can create an group of nested packages with the following structure.
/websites/abundantcode

When you place your sourcecode inside the package must have package name mentioned as the first line in the file. For example , if the main application is named as  abundantcodeconsole under the package abundantcode , you will have the sourcecode as shown below.

package abundantcode;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class abundantcodeconsole {
    public static void main(String[] args) {

        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter data");
        try {
            String data = input.readLine();
            System.out.println(data);
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }

        Boolean b1 = Boolean.valueOf("false");
        System.out.println(b1);

    }

}

The file actually resides in the folder abundantcode and the path of the abundantcodeconsole.java is as shown below.

/websites/abundantcode/abundantcodeconsole.java

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: