Static constants

Posted on 2006-04-07
Last Modified: 2010-03-31
I've got a class that looks like this:

public final class C
     // M A T H   C O N S T A N T S
     public static final float TO_RADS = 0.017453292f ;
     public static final float TO_DEGS = 57.29577951f ;
     public static final float PI = 3.141592654f ;
     // etc ..

I was just wondering - will the VM store _all_ of the static members in memory as soon as just one of them has been used, or will it only store/load each one after that one has been used?

So, if I were to just use PI within my program, would the VM still load all of the other variables? (Seeing as I actually have a pretty large list)..

Question by:InteractiveMind
    LVL 92

    Accepted Solution

    they would all get loaded when the class is loaded (as they are class vartiables)
    What is your concern?

    btw, The Math class already has PI
    LVL 25

    Author Comment

    > What is your concern?
    Well, my list of variables is quite large as it is, and I will probably expand it at some stage... But some programs that use this class will only use 1 or 2 of these variables - so I was wondering about memory being wasted.
    (Mind you, I suppose it's not significant on today's machines — but I was interested to know anyway :-)).

    > btw, The Math class already has PI
    Yer, but doesn't that class actually use some algorithm to calculate the value of PI on first use, or something?
    Besides, I need it as a float, and it saves me casting.  :-)

    LVL 92

    Expert Comment

    > Yer, but doesn't that class actually use some algorithm to calculate the value of PI on first use, or something?

    don't think so, think it uses 3.141592653589793
    LVL 25

    Author Comment

    Hm. Ah well.. it saves me the explicit cast  :-)

    Thank you

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    method argument as final 1 54
    count8 challlenge 13 68
    parentbit challenge 3 37
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now