Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Properly declaring constants in Java

  1. #1
    Forum VIP
    Join Date
    Jun 2011
    Posts
    232
    My Mood
    Stressed
    Thanks
    36
    Thanked 66 Times in 54 Posts
    Blog Entries
    4

    Default Properly declaring constants in Java

    I have always declared constants like so;

    private static final int MY_CONSTANT = 0;

    But recently I have had reason to question the use of the static keyword in this declaration. Since it is final, do I even need to declare it static? Are there any repercussions for leaving out static? Are there any reasons I should leave in the static modifier in this context? Are there any costs associated with leaving the static clause?


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Properly declaring constants in Java

    The repercussion of static in this context is a) you do not need to instantiate the class to get the value b) the value remains identical across classes c) Given static defines a class variable, it is not created with every instance of the class you create. Taking that forward:

    Since it is final, do I even need to declare it static?
    If you access it from within a static context (for instance methods), then yes, and your compiler will tell you so.

    Are there any reasons I should leave in the static modifier in this context?
    Again, do you need to access the variable from within a static context?

    Are there any costs associated with leaving the static clause?
    Costs insofar as how you access the variable, and (albeit most likely miniscule and perhaps optimized by the compiler and/or JRE) costs of overhead when declared non-static. From the other perspective of not declaring it static, is there a reason you wish to have this variable for every instance?
    Last edited by copeg; March 19th, 2012 at 12:29 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    ChristopherLowe (March 20th, 2012)

  4. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Properly declaring constants in Java

    Are there any repercussions for leaving out static?
    The biggest one imaginable: if you live your life by the Java Code Conventions then you would have to change the case of your name:

    Code Conventions for the Java Programming Language: 9. Naming Conventions

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    ChristopherLowe (March 20th, 2012)

Similar Threads

  1. Abstract Classes and Named Constants
    By snowguy13 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 7th, 2012, 01:43 PM
  2. Java Child process cannot execute properly for telnet command
    By mamunbu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 07:43 AM
  3. Declaring your own exceptions
    By weakprogrammer in forum Exceptions
    Replies: 4
    Last Post: June 30th, 2011, 08:19 AM
  4. Java Newbie - Sorted Linked List not inserting properly - please help!
    By bubbleboy in forum What's Wrong With My Code?
    Replies: 20
    Last Post: June 17th, 2011, 11:48 AM
  5. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM