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 5571 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

Leave a Comment


x

You might be Interested in these Posts

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford http://www.youtube.com/watch?v=sfV0ApyN5fM ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...

Video - Getting started with Angular 2

In this screencast , Alex Eagle , developer on the Angular team talks about Angular 2 developer prev...