1

I have been trying to understand what is going on in this piece of Java code, but so far I didn't manage. This is part of this library .

My wonders are on

CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(Constants.VAR_NET_BOUNDING_BOX)

They do not appear previously in the code and later on, there is one constructor which uses CURRENT_SIM_TIME without any brackets ().

enum Variable {
        CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(
                Constants.VAR_NET_BOUNDING_BOX), ;
        public final int id;
        private Variable(int id) {
            this.id = id;
        }
}

This the constructor I was talking about:

SimulationData(DataInputStream dis, DataOutputStream dos) {
        super("", Variable.class);
        addReadQuery(Variable.CURRENT_SIM_TIME,
                new ReadObjectVarQuery.IntegerQ(dis, dos,
                        Constants.CMD_GET_SIM_VARIABLE, "",
                        Variable.CURRENT_SIM_TIME.id) {

                });

        addReadQuery(Variable.NET_BOUNDARIES,
                new ReadObjectVarQuery.BoundingBoxQ(dis, dos,
                        Constants.CMD_GET_SIM_VARIABLE, "",
                        Variable.NET_BOUNDARIES.id));

        this.dis = dis;
        this.dos = dos;

    }

So, how is this possible?

Inside the enum Variable we have CURRENT_SIM_TIME and NET_BOUNDARIES with an argument inside the brackets and then in the constructor we create an addReadQuery with Variable.CURRENT_SIM_TIME as well as Variable.NET_BOUNDARIES, without brackets as an argument.

Are these functions? Static variables? Are they be defined in some other part of the code and I am not able to find them?

I am really lost right now...

Terahertz
  • 64
  • 12
  • 1
    Java enums allow such constructs; they are basically classes: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – CoolBots May 11 '16 at 09:37

1 Answers1

2

Declaring Enum :

enum Variable {
        CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(
                Constants.VAR_NET_BOUNDING_BOX), ;
        public final int id;
        private Variable(int id) {
            this.id = id;
        }
}

So when you say CURRENT_SIM_TIME(Constants.VAR_TIME_STEP) inside your enum declaration, you are calling the constructor Variable(int id) such that for enum CURRENT_SIM_TIME the value of the id variable will be set to Constants.VAR_TIME_STEP for that enum itself.

Similar is the case for NET_BOUNDARIES

So if you want to get the value of id for CURRENT_SIM_TIME, you would do it like :

Variable.NET_BOUNDARIES.id

Using Enum

And when you use enum, you don't have to call it with constructor, because that's already done when you declare your enum. So you just use the enum.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • Ok! Now I undestand it! Just one comment. I am trying to create a C# equivalent for this, so would you recommend me doing the conversion as shown in this thread? http://stackoverflow.com/questions/469287/c-sharp-vs-java-enum-for-those-new-to-c – Terahertz May 11 '16 at 09:49
  • 1
    You could do it depending on your requirement, but that will be only a method for enum like what you have for classes, which you could call like `Variable.NET_BOUNDARIES.someMethodDefinedInVariableEnum()` – Abubakkar May 11 '16 at 09:55