Java – How to use Packages to organize code ?

Published on March 13, 2016 by abundantcode

Filed under Java

Last modified March 13, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 3378 times

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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C Program to display Inverted half-pyramid using *

Problem Write a program in C to print Inverted half-pyramid using * as shown below.The program shou...

How to use array Rank in C# ?

In C# , the Rank property of the array is is used to get the rank of the array. In simple terms , ra...

How to append an array to another in JavaScript ?

In JavaScript , you may want to append one array to another array(s) to combine their elements resul...