Enum in Java

This is just a quick post because I just decided to add an enum type class into my Student object. I’m not sure how the best way to describe them but think of them as variables with pre-determined values. I mostly seem them mostly used as states or bit flags. I have some C# examples if you want to see how a state machine or bit flags works. In this case it’s super basic example that just determines if the current status of the student.

public class Student {
        public enum Status {
	        non_student,	// Not enrolled at this school.
	        active,         // Currently enrolled at this school.
	        dropOut,        // Drop Out from this school.
	        transfered,     // Transfered from this school.
	        alumni;         // Graduated from this school.
        private Status status;
        private int id;
        private double gpa;
        private String firstName, lastName;
        private char middleInitial;

Pretty much this just a nested class within my Student object. Since it’s a public variable you can call the class outside of the Student class to make a new Status for the student. Since my variable status is private you will need make a setter like this.

public void setStatus( Status status) { this.status = status; }

Outside the Student object you can reference the setStatus method like this.

someStudent.setStatus( Status.active );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s