Home > DeveloperSection > Forums > Can anybody say my below code is a variable, function or something else
Madam Walker
Madam Walker

Total Post:22

Points:154
Posted on    October-14-2013 3:29 AM

 Java Java 
Ratings:


 1 Reply(s)
 655  View(s)
Rate this:

I was going through some application's source code and I came across this piece of code and I am not able to understand what this is.

public enum XStreamUserType {
MES_CONFIG_XSTREAM(MESConstants.MES_CONFIG_XSTREAM_USER, "MES_CONFIG_XSTREAM_USER",    new XStream());
private int userTypeId;
private String userType;
private XStream xStream;
private XStreamUserType(int userTypeId, String userType, XStream xStream) {
    this.userTypeId = userTypeId;
    this.userType = userType;
    this.xStream = xStream;
    switch (this.userTypeId) {
    case MESConstants.MES_CONFIG_XSTREAM_USER:
        loadConfigReader();
        break;
    default:
    }
}

What does this line mean?

MES_CONFIG_XSTREAM(MESConstants.MES_CONFIG_XSTREAM_USER, "MES_CONFIG_XSTREAM_USER",    new XStream());


Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    October-14-2013 4:02 AM

In java enum can also have instance variables and constructors like a class.

Here enum XStreamUserType has three instance variables

private int userTypeId;
private String userType;
private XStream xStream;

while

private XStreamUserType(int userTypeId, String userType, XStream xStream) {
    this.userTypeId = userTypeId;
    this.userType = userType;
    this.xStream = xStream;
    switch (this.userTypeId) {
    case MESConstants.MES_CONFIG_XSTREAM_USER:
        loadConfigReader();
        break;
    default:
    }
}

is constructor.

MES_CONFIG_XSTREAM is enum variable(can be interpreted as object of a class), initialized as MES_CONFIG_XSTREAM(MESConstants.MES_CONFIG_XSTREAM_USER, "MES_CONFIG_XSTREAM_USER", new XStream()); with values to its instance variables(as a constructor call).


Don't want to miss updates? Please click the below button!

Follow MindStick