Use constant variable declaration across your package in JAVA
Tags: java, oops, packag
Published: 2021-05-16 16:30:09

## 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:
```java
if(order.status == "PENDING"){
   // do something
}else if(order.status == "COMPLETED"){
//
}else more...

// we can also use switch statement here...
```

## 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](https://www.w3schools.com/whatis/whatis_json.asp#:~:text=JSON%20stands%20for%20JavaScript%20Object,describing%22%20and%20easy%20to%20understand) . 

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**
```java
package events;

public class Const {
	final static String PENDING = "PENDING";
	final static String IN_PROGRESS = "IN_PROGRESS";
	final static String COMPLETED = "COMPLETED";
}
```
```js
// output
COMPLETED
IN_PROGRESS
PENDING
```

In the same package, we can have another class for testing.
```java
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);
	}
}
```
```ja
// output
COMPLETED
IN_PROGRESS
PENDING
```
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`.
```java
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();
}
```
You can see `ORDER` member has a reference to `Event` instance. So we can use it like below.
```java
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);
}
```

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