Edit: My solution is pretty useless since as quew8 pointed out you can use a private constructor.

I was wondering how to group ints in a way which would allow me to create a type-safe argument for a constructor/method. At first I didn't think this was possible without using a switch statement with an enum, so I just grouped them in nested classes, but today I was playing with enums (I never really use them much) and found that I could do this. E.g, if you want to group OpenGL data types you can do this.

Out of interest, what do you mean that this is type safe? As opposed to what which wasn't?

The function

glBindBuffer(inttarget, inthandle)

accepts an int value as the target. You could pass in 6382 which would result in an OpenGL error, as it only accepts a selection of OpenGL values, e.g GL_ARRAY_BUFFER. If you encapsulated that function in a method which only accepts those values (which is what the enum is for), then it's effectively (though not truely) making it type-safe. You cannot pass in a value such as 6382 as it would result in a compile time error.

You don't need to have an anon class per enum entry in this case...just pass in the target value to a constructor.

I thought of doing that but this approach allows you to name the entries whatever you like which is what I wanted to do. EDIT: That would also eliminate the type-safety as you could similarly pass in 343278 to the constructor which would result in a runtime error.

Oh I see. Maybe call it GLEnum safe or something rather than type safe to avoid confusion with Java generics.

And what @Roquen means is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

publicenumDataTypeBYTE(GL_BYTE), SHORT(GL_SHORT);

privateAxis(intenum) {this.enum = enum; }

privatefinalintenum;

publicintgetEnum() {returnenum; }}

You can still name the entries whatever you like.

I think the problem will come when you need to be able to reuse an GLEnum in more than one function (which I'm sure you have to occasionally although I can't think of one now). Also wouldn't work for bit fields. Don't get me wrong I'm not trying to pick holes, just encouraging preemptive problem solving.

Enums use a private constructor. They are a fixed set of compile time values so the "user" cannot construct different ones without going into the source, changing it and recompiling. If he did that, I don't think he'd have to look far to solve the problem.

Oh I can't believe that I've never came across an enum with a private constructor before. Every enum I've encountered before has either had a public constructor or hasn't declared one. My solution is pretty useless then.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org