aspose file tools*
The moose likes Beginning Java and the fly likes Define constants ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Define constants !" Watch "Define constants !" New topic
Author

Define constants !

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
I want to know that how to define constants in java?
In C , we define like , #define ABC
BUt how in java?
Thanks
Angela
vasu kalya
Greenhorn

Joined: Mar 09, 2001
Posts: 6
The only way I know of is defining static variables.
For eg: public static int ABC = 1;
and then use that in your program as
int myValue = 1;
if (myValue == ABC)
{
System.out.println("This value is indeed one..");
}
...
hope it helps.
-vasudeva
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks Vasu,
But this is within oone class, How abt if I declare or define constant value in one class and i want to access that value in another public class in separate file?
Thanks again,
Angela
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
If you declare a variable final and static it becomes a compile time constant.
For instance the Math class has 2 contants, pi and e.
public static final double PI = 3.14159265358979323846;
public static final double E = 2.7182818284590452354;
You can use them by saying Math.PI or Math.E (because they are static you don't need to create an instance of Math to use them).


"JavaRanch, where the deer and the Certified play" - David O'Meara
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks a bunch Cindy!
Angela
usman ahmad
Ranch Hand

Joined: Mar 06, 2001
Posts: 46
Hi!
sometimes a variable or a datafield should be constant i.e. it should not change during the execution of a programme.For example:
The value for a company id is fixed, so you do not want any methods to alter the company id value while a programme is running. To prevent alteration, insert the key word "final" in the company id variable declaration so the name
COMPANY_ID becomes a symbolic constant, which indicates that when you compile any program that uses an object that contains the COMPANY_ID, the field has a final, unalterable value.
You cannot change the value of a symbolic constant after declaring it, any attempt to do so will result in a compile time error.
the class java.lang.Math contains constants and methods that you can use to perform common mathematical functions.
As described by Cindy,
public final static double PI = 3.14159265358979323846;
NOTICE that PI is
1)public ,so any program can access it directly.
2)final, so it cannot be changed
3)static, so only one copy exists
4)double, so it holds a large floating point value.
I hope this is helpful.
Regards
Usman
Steve Durber
Greenhorn

Joined: Aug 05, 2004
Posts: 9
I think this has solved my question also, apart from
Where is the best place to hold the static variables?

Do they go in main or one of the classes, or do you create a seperate class for them?
[ August 16, 2004: Message edited by: Steve Durber ]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
Put the final static (constants) in the class that seems the most logical to be responsible for them.
Try imagine you had all your classes listed in front of you and decide where you would look first to find a value with a particular name.
You could also create a class just to hold a bunch of related values such as "PayrollConstants"; in this case you might want to give that class a private constructor so users can better see that it is meant expressly and only for storing final static values.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
This recent thread from the Intermediate forum answers the question very well in my opinion. The answer from Marilyn de Queiroz is the definitive one.

Jules
[edited to spell Marilyn's name right. D'oh! ]
[ August 16, 2004: Message edited by: Julian Kennedy ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Define constants !