Java Enum examples of preserving String literal values

Posted by

At work I’ve come across to hold String literal values to treat them as variables. Normally I would use an Interface and define them there or a separate class same way set the variables as final static String and mark it as public to make it visible across project.

I was looking for something very convenient and keep it in my domain class where all properties are defined as private. So I’ve come to the conclusion of implementing enum. The below i am exposing two simple examples for your String or any literal holding purposes.

 Solution One:

TableStatistics.java

public class TableStatistic {

	private String tableName;
	private String[] columns;
	private String clauseType;

	public enum ClauseTypes {
		SELECT, WHERE
	};

	public TableStatistic() {
		super();
	}

	public TableStatistic(String tableName, String[] columns, String clauseType) {
		super();
		this.tableName = tableName;
		this.columns = columns;
		this.clauseType = clauseType;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String getClauseType() {
		return clauseType;
	}

	public void setClauseType(String clauseType) {
		this.clauseType = clauseType;
	}

	@Override
	public String toString() {
		return "TableStatistic [tableName=" + tableName + ", columns="
				+ Arrays.toString(columns) + ", clauseType=" + clauseType + "]";
	}

}

Usage of the literal value

tableStatistic.setClauseType(TableStatistic.ClauseTypes.SELECT.name());

Basically here the enum value will be directly returned. How about we want to return a different String literal value?

The below example will be showing you how to

Solution Two:

TableStatistics.java

public class TableStatistic {

	private String tableName;
	private String[] columns;
	private String clauseType;

	public enum ClauseTypes {
		SELECT {
			public String toString() {
				return "SELECT";
			}
		},
		WHERE {
			public String toString() {
				return "WHERE";
			}
		}
	};

	public TableStatistic() {
		super();
	}

	public TableStatistic(String tableName, String[] columns, String clauseType) {
		super();
		this.tableName = tableName;
		this.columns = columns;
		this.clauseType = clauseType;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String getClauseType() {
		return clauseType;
	}

	public void setClauseType(String clauseType) {
		this.clauseType = clauseType;
	}

	@Override
	public String toString() {
		return "TableStatistic [tableName=" + tableName + ", columns="
				+ Arrays.toString(columns) + ", clauseType=" + clauseType + "]";
	}

}

Usage of the literal value

tableStatistic.setClauseType(TableStatistic.ClauseTypes.SELECT.name());

As you notice. Each enum holds a customized literal value

Leave a Reply

Your email address will not be published. Required fields are marked *