• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Learning about constants

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already read about constants I have saw an example using PI calculate:



But when do I need to use, how to use and how to invoke a constant?
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote: . . .
. . .
4.141592f, surely?
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, 3.141592f. It's pi for pete's sake. You guys suck at math!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:No, 3.141592f. It's pi for pete's sake. You guys suck at math!


I got it right in my post. Do I get a gold star ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I thought it was pie
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:No, 3.141592f. It's pi for pete's sake. You guys suck at math!

well...it's APPROXIMATELY 3.141592f.

I assume nobody has mentioned Math.PI since the OP is trying to figure out how to do this on his own.

FWIW, that is defined as 3.141592653589793d
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Rob Prime wrote:No, 3.141592f. It's pi for pete's sake. You guys suck at math!


I got it right in my post. Do I get a gold star ?

You, always. I'll just take one of Campbell's.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well , in summary, if you want a class-wide constants ,then simply declare it as static final ,otherwise eliminate static modifier.
 
Abhishk Gupta
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . Yes, each new object can have its own value of pi. . . .
Even 4.141592f
 
Abhishk Gupta
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's 3.14159265358979323..., so in fact the correct number is 3.141593.

I'm just sayin'...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote: . . . I'm just sayin'...
I'm just annoying
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic