Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By quad64bit

Thread: constant in object ??

  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default 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;
    instead of

    Java Code:
    final static int SLOW = 1;
    final static int MEDIUM = 2;
    final static int FAST = 3;
    Isn't that constant should use final instead of public?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

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

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default 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;
    But actually, using an enum would have been the better solution.

    Regards,
    Jim
    Last edited by jim829; 09-17-2013 at 06:10 PM. Reason: Poor grammar
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.
    dojob likes this.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: constant in object ??

    I'm impressed that you understood what I said. My grammar was horrible (has since been corrected).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

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

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. While statement with a constant condition
    By kkid in forum New To Java
    Replies: 5
    Last Post: 08-28-2013, 10:31 PM
  2. Is there a way to emit a constant frequency?
    By Vinx in forum New To Java
    Replies: 9
    Last Post: 07-05-2012, 01:01 AM
  3. Cannot use as static constant
    By KiranA in forum Eclipse
    Replies: 1
    Last Post: 12-01-2011, 09:19 AM
  4. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 07:51 AM
  5. constant variables questions
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •