Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Define constants !

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch Cindy!
Angela
 
usman ahmad
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic