Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Java: Declaring constants, pre-interpreted

I'd like to declare an absolute constant in Java code, like C++ const
that avoids calculation in the interpreter, and is automatically directed in byte code.

so, in regular expression considerations, requires no look-ups.
so, I declare the constant as a field in my class, and it is correct

?

Thx
Avatar of for_yan
for_yan
Flag of United States of America image

Maybe you are talking about
something like that:

final static num = 5;

>so, in regular expression considerations, requires no look-ups.
>so, I declare the constant as a field in my class, and it is correct

what is it that you mean by the above, please elaborate
Avatar of James Hancock

ASKER

looks like it's

(public/private) static final int variable = value;

seems to work

Of course the type (int) is also necessary, just forgot

final static int num = 5;

I think it is initialized with loading of the class
Avatar of CEHJ
You might like to consider enum, depending on what you're doing, as int cannot be type-safe
Do you mean, vindictive user classes that extend from my main class might mess up final data?

If I state private final for a main class, can sub-classess that inherit, alter private data?

I thought extends   means  is-a

What is type safe?

I am just making the x and y sizes for a rectangle in my game completely constant.

Any vindictive class that altered that would be undesirable - , I don't want it changeable.

How simple is it to backwards engineer byte code into understandable human code?
I can't put anything past determined hackers.

Is FINAL data put compiled in to the byte code as that value?

If you declare the value private it will not be accessible by subclasses
(only protected will).
 
extends means IS A - you are write

and final static will be good for your rectangle

If you don't want your class to be subclassed you can make it final
(as final valure and final class have different meaning).

But I don't think you really need it.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>What is type safe?

It means, e.g. that a method to which you might pass your constant value would quite as easily accept an arbitrary value. That is not the case when it can only accept one, final type

look at this interesting discussion relevant to your question about
Is FINAL data put compiled in to the byte code as that value?

http://stackoverflow.com/questions/3524150/is-it-possible-to-disable-javacs-inlining-of-static-final-variables
Great.

Could a malicious, deviant coder extract from the byte code, data members that never change, so as to identify crucial data member locations, values and change them?
Theoretically, it is always possible.

 
Such things are easily extracted with a Java decompiler, yes
Not so much as

- Each user will implement a class that can obtain game data from a server - but will only be able to access the remote server through my own access methods. So, their work will be pre-compiled.

So, the local client will store / determine game variables, so, if I declare data private, can deviants get around data visibility in the interpreter?  And spy on their opponent's moves? - The play will be run on my own machines, not theirs.

I may do code reviews of submitted work. How can you cheat visibility in regular code?
>> How can you cheat visibility in regular code?

One simple way is to load the class in question and get its private members through reflection, so the moral of the story is: don't store sensitive info inside a java binary (or actually ANY binary ultimately)
Would that require any unusual .h imports ? How is reflection done?  I'll say, only usual .h imports are allowed, eg util, awt, swing
The information I want to protect is present only at run-time and I'll make it all private data members, not public variables.

What's the point of private, if it can be cheated?

With reflection you'll need to import
java.lang.reflect.something

There is no doubt that everything can be hacked and unlocked
Point of private is not agains hackers - it is against folks that make mistakes



private is for your fellow programmers working on another part of the application
or for say customers who bought your class library and are using it - so that they could not take your class
and say
YourClass.mainValue = 0;
and with that screw up the way your library works and then complain that your library is not working

For the hacker there is not much difference between private and public.
How could a hacker possibly write Java code to steal private x,y integer locations of other units in the game. - without being astonishingly obvious.
I thought the VM didn't allow that kind of thing. - ignoring private. The compiler doesn't allow it?

Any classes submitted to me could be almost considered fellow programmers, I guess, since it is all on the same application.
What do you think I might enforce a ban on for deviants to make mischief? (who will get caught)
I already plan to have code reviews.

Obviously, no external link includes, only submitted code. No reflection (import banned)
Anything else for spying?