As with many things, there are many ways to do this. One thing you shouldn't do is declare them several times - it's just plain stupid .: P
Everything should be in a class in Java, so either:
- Select the "main" class (let's say I have a project called "FTPServerApp" - I could post them there)
- Create a class "Util" containing all of them
When you figure out where to put them, declare them this way:
public static final [type] [NAME_IN_ALL_CAPS] = [value];
It will be
- make them available for all project code anywhere (
public
) - only one copy of the value exists in all instances of the class (
static
) - they cannot be changed (
final
).
ALL_CAPS_FOR_CONSTANT_NAMES
, separated by underscores, is a symbol in Java.
So, if this was declared in a class called FTPServerAPP
, and you had a constant called SERVICE_PORT
, it could be:
public class FTPServerApp { public static final int SERVICE_PORT = 21; ... }
... and you will access it from any class, for example ...
FTPServerApp.SERVICE_PORT
source share