• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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
0
beavoid
Asked:
beavoid
  • 10
  • 7
  • 4
1 Solution
 
for_yanCommented:
Maybe you are talking about
something like that:

final static num = 5;
0
 
for_yanCommented:

>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
0
 
for_yanCommented:
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
beavoidAuthor Commented:
looks like it's

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

seems to work
0
 
for_yanCommented:

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
0
 
CEHJCommented:
You might like to consider enum, depending on what you're doing, as int cannot be type-safe
0
 
beavoidAuthor Commented:
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?
0
 
for_yanCommented:

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.
0
 
for_yanCommented:


f you declare the value private it will not be accessible by subclasses
(only protected will).
(that was not very good wording  - of course public and default will be accessible  also)
0
 
CEHJCommented:
>>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
0
 
for_yanCommented:

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
0
 
beavoidAuthor Commented:
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?
0
 
for_yanCommented:
Theoretically, it is always possible.

 
0
 
CEHJCommented:
Such things are easily extracted with a Java decompiler, yes
0
 
beavoidAuthor Commented:
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?
0
 
CEHJCommented:
>> 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)
0
 
beavoidAuthor Commented:
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?

0
 
for_yanCommented:
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

0
 
for_yanCommented:


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.
0
 
beavoidAuthor Commented:
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.
0
 
beavoidAuthor Commented:
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?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now