Searching...
Sunday, 14 May 2017

Enum Constructor example

17:26

Enums in java can have Constructor also to pass data while creating Enum constants. In this example we are trying get the maximum mark of a subject while passing the subject code.

package com.vinod.test;


/**
 *@authorvinodkariyathungalkumaran
 *
 */
public class EnumConstructorExample {
    public static void main(String[] args) {
        for (MaxMark m : MaxMark.values()) {
            System.out.println("Subject code=" + m + " Maximum Mark " + m.getMaxmark());
        }
        System.out.println("Physics Maximum Mark=" + MaxMark.P.getMaxmark());
    }
}


enum MaxMark {
    P(50), C(50), B(75), M(100);
    private int maxmark;
    MaxMark(int p) {
        maxmark = p;
    }
    int getMaxmark() {
        return maxmark;
    }
}

Output

Subject code=P Maximum Mark 50
Subject code=C Maximum Mark 50
Subject code=B Maximum Mark 75
Subject code=M Maximum Mark 100
Physics Maximum Mark=50
Next
This is the most recent post.
Older Post

0 comments: