File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Learning about constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Learning about constants" Watch "Learning about constants" New topic
Author

Learning about constants

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3502
    
  14
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
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
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
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

Joined: Oct 13, 2005
Posts: 38511
    
  23
André Asantos wrote: . . .
. . .
4.141592f, surely?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

No, 3.141592f. It's pi for pete's sake. You guys suck at math!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3502
    
  14
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

Joined: Oct 13, 2005
Posts: 38511
    
  23
And I thought it was pie
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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

Joined: Aug 31, 2004
Posts: 11343

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: 38511
    
  23
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
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
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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: 38511
    
  23
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
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's 3.14159265358979323..., so in fact the correct number is 3.141593.

I'm just sayin'...


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
Ernest Friedman-Hill wrote: . . . I'm just sayin'...
I'm just annoying
 
 
subject: Learning about constants