Results 1 to 7 of 7
Thread: constant in object ??
- 09-17-2013, 01:30 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
constant in object ??
Design a class named Fan to represent a fan. The class contains:
a) Three constants named SLOW, MEDIUM and FAST with value 1, 2, and 3 to denote the fan speed.
b) An int data field named speed that specifies the speed of the fan(default SLOW).
c) boolean data field named on that specifies whether the fan is on(default false).
.....
I would like to know why the sample code given is
Java Code:public static int SLOW = 1; public static int MEDIUM = 2; public static int FAST = 3;
Java Code:final static int SLOW = 1; final static int MEDIUM = 2; final static int FAST = 3;
- 09-17-2013, 01:50 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Re: constant in object ??
It can also be public final static int. But, yes, technically, if it is to be called a "constant" it SHOULD be final.
- 09-17-2013, 01:51 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: constant in object ??
Not if all the world is supposed to see it. Besides, comparing public to final doesn't
make sense as they mean different things. It could have been specified as
Java Code:public final static int SLOW = 1;
Regards,
JimLast edited by jim829; 09-17-2013 at 07:10 PM. Reason: Poor grammar
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 06:54 PM #4
Re: constant in object ??
I agree with Jim on both counts. To add to the example you provided, in the first the scope is explicitly declared public meaning that any code anywhere has access to it. This makes sense for certain constants like Math.PI. In the second example, the scope isn't defined which means it is implicitly package-private (the default java scope). This means that any classes in the same package will have access to it, but not from outside. This may or may not be what you want.
- 09-17-2013, 07:11 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: constant in object ??
I'm impressed that you understood what I said. My grammar was horrible (has since been corrected).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 07:54 PM #6
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
Re: constant in object ??
After seeing these replies, I am thinking that perhaps I did not ask the question correctly.
The main idea that I would like to ask is that why final is not used in this case since the question stated that it is constant.
- 09-17-2013, 09:02 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: constant in object ??
It was already answered. It should be declared final. Sometimes examples don't necessarily do the obvious because they are trying to drive home a different point. Or they simply make a mistake.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
While statement with a constant condition
By kkid in forum New To JavaReplies: 5Last Post: 08-28-2013, 11:31 PM -
Is there a way to emit a constant frequency?
By Vinx in forum New To JavaReplies: 9Last Post: 07-05-2012, 02:01 AM -
Cannot use as static constant
By KiranA in forum EclipseReplies: 1Last Post: 12-01-2011, 10:19 AM -
Constant Variables
By harshakantha in forum New To JavaReplies: 3Last Post: 06-13-2011, 08:51 AM -
constant variables questions
By sgthale in forum New To JavaReplies: 3Last Post: 05-06-2011, 06:34 AM
Bookmarks