• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Learning about constants

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

André Asantos wrote: . . .
. . .

4.141592f, surely?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I thought it was pie
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow...why it didn't strike me

Anyways Congrats folks:thumbup:
finally your cummulative efforts changed the value of pi
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's 3.14159265358979323..., so in fact the correct number is 3.141593.

I'm just sayin'...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote: . . . I'm just sayin'...

I'm just annoying
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic