DEV Community

Rahul kumar
Rahul kumar

Posted on

Use constant variable declaration across your package in JAVA

Situation

Suppose we are working on a food ordering application, where we need to define some status of the order for user to track. Ex, Pending, in-progress, completed, etc.

For instance suppose we have an order object which contains information about order, like date, price, and status.

For this example we'll only consider status member of order

Now, we have order and we would like to know the status of the order then typically we do:

if(order.status == "PENDING"){
   // do something
}else if(order.status == "COMPLETED"){
//
}else more...

// we can also use switch statement here...
Enter fullscreen mode Exit fullscreen mode

Problem

The problem with this approach is that, we always need to remember and type everything by hand and there is a big chances of typing mistake. Suppose, we have 100's of events, then it would be tedious to work with them.

Solution for JavaScript Developers

If you are coming from a JavaScript background, then you might have heared of JSON. You can read more about JSON .

With JSON we can create a JSON object to hold these contants, and intellisense can help a lot.

We can also do this with plain JavaScript object.

Solution for JAVA

This solution is much batter than JavaScript, because it doesn't require to import in java(package helps us).

Create a Class for holding constants

package events;

public class Const {
    final static String PENDING = "PENDING";
    final static String IN_PROGRESS = "IN_PROGRESS";
    final static String COMPLETED = "COMPLETED";
}
Enter fullscreen mode Exit fullscreen mode
// output
COMPLETED
IN_PROGRESS
PENDING
Enter fullscreen mode Exit fullscreen mode

In the same package, we can have another class for testing.

package events;

public class Test extends Print{
    public static void main(String[] args) {
        System.out.println(Const.COMPLETED);
        System.out.println(Const.IN_PROGRESS);
        System.out.println(Const.PENDING);
    }
}
Enter fullscreen mode Exit fullscreen mode
// output
COMPLETED
IN_PROGRESS
PENDING
Enter fullscreen mode Exit fullscreen mode

Because, COMPLETED, IN_PROGRESS and PENDING are static member, so we can refer them directly by class name.

Anytime, we don't need to type full name of the events because intellisense of IDE will help us a lot in this situation.

What about nested constants?

Suppose we have a constants for order specific, then we can either create another class like Const or we can create nesting like below.

Let's have another class Event.

package test;
class Event{
    final String NEW_ORDER = "NEW_ORDER";
}
public class Const{
    final static String PENDING = "PENDING";
    final static String IN_PROGRESS = "IN_PROGRESS";
    final static String COMPLETED = "COMPLETED";
    final static Event ORDER = new Event();
}
Enter fullscreen mode Exit fullscreen mode

You can see ORDER member has a reference to Event instance. So we can use it like below.

public static void main(String[] args) {
        System.out.println(Const.COMPLETED);
        System.out.println(Const.IN_PROGRESS);
        System.out.println(Const.PENDING);

        // new order
        System.out.println(Const.ORDER.NEW_ORDER);
}
Enter fullscreen mode Exit fullscreen mode

Comments if you found any suggestion or mistakes or any other methods better than this.

Top comments (2)

Collapse
 
climbing_dev profile image
Maciej Modzelewski

Hey Rahul, consider using enum for such cases: enum OrderStatus { COMPLETED, IN_PROGRESS, PENDING }. This gives you better type saftety and adds more context to the contants.
Even better you could create an internal enum in the Order class and expose methods like: isPending(); isInProgresss(); isCompleted(). This way the status does not need to leak to the rest of the application.

Collapse
 
ats1999 profile image
Rahul kumar

hey Maciej, you are right we can use enum for such case. But, we can also expose methods as you said.

This way the status does not need to leak to the rest of the application

We are just exposing the status codes across the package, not the status of any particular order. We can always hide the status of order from outside entity.