I hope that's not your final definition of pi, because it's off by 1.
You use constants for anything that, well, needs to stay constant. This could be anything, from a maximum value (like Integer.MAX_VALUE) to a hard coded name for something (like BorderLayout.CENTER).
They are also normally defined as static, so that you can access them using the class name and don't need to create an instance of the class
Joanne
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
posted
0
Be careful about the static part since you may want a different final value
in each object, to track its instance number, for example, or capture its
time of creation.
Jim ... ...
BEE MBA PMP SCJP-6
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
0
Jim Hoglund wrote:Be careful about the static part since you may want a different final value
in each object, to track its instance number, for example, or capture its
time of creation.
But wouldn't it then not be a constant?
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
posted
0
pete stein wrote:But wouldn't it then not be a constant?
Not necessarily. Static means that the variable or reference would be associated with the class, and not an individual object. Making the variable or ref final would mean that you could have a constant for each object.
Rob Prime wrote:I hope that's not your final definition of pi, because it's off by 1...
Actually, it's only off by 4.141592 - pi, which is in the general neighborhood of 0.9999993464102067615373566167205. Far less than 1.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
fred rosenberger wrote: . . . I assume nobody has mentioned Math.PI since the OP is trying to figure out how to do this on his own. . . .
No, I thought it was because he insisted on using a float.
Tracy Tse
Greenhorn
Joined: May 24, 2010
Posts: 22
posted
0
well , in summary, if you want a class-wide constants ,then simply declare it as static final ,otherwise eliminate static modifier.
Whatever it takes is what i have got !
Lift is the best sitcom!
Abhishk Gupta
Greenhorn
Joined: Oct 08, 2008
Posts: 22
posted
0
wooohh....That was quite a discussion but allow me to divert it
John de Michele wrote:
pete stein wrote:But wouldn't it then not be a constant?
Not necessarily. Static means that the variable or reference would be associated with the class, and not an individual object. Making the variable or ref final would mean that you could have a constant for each object.
John.
Does this mean, I can change the value of pi with each new object?
Please elaborate what you mean when you say "you may want a different final value
in each object"
Abhee
There are only 10 types of people in the world: those who understand ternary, those who don't, and those who mistake it for binary.
Abhishk Gupta wrote:Does this mean, I can change the value of pi with each new object?
Yes and no.
Yes, each new object can have its own value of pi. No, you cannot change it after it has been set, and it needs to be set either when declared or in the constructor:
If you initialize it when declaring it then all instances will have the same value and you should turn it into a static field. If you forget to initialize it in the constructor (actually each constructor) the code will not compile.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
Rob Prime wrote: . . . Yes, each new object can have its own value of pi. . . .
Even 4.141592f
Abhishk Gupta
Greenhorn
Joined: Oct 08, 2008
Posts: 22
posted
0
wow...why it didn't strike me
Anyways Congrats folks finally your cummulative efforts changed the value of pi
Ernest Friedman-Hill
author and iconoclast
Marshal