|
|
|
|
You may:
or:
The latter is preferred, especially if the instance variable is typically being initialized to the same value every time. double m_radius = 0.0; int m_countDownStart = 10; You may also declare an instance variable as final. Here's an example from the Java Math class: public final static double PI = 3.14159265358979323846; final means that it's the 'final value' -- in other words, it's a constant, and once initialized can't be changed. final variables must be initialized; if you forget: static final double m_interestRate; you'll get an error: 'final' members must be initialized Such final instance variables are initialized when the instance is created. JDK 1.1 introduced the concept of blank finals, which allow you to not include an initializer on the declaration of a final variable, but that variable must still have a value assigned to it before it is used. I doubt you'll find a reason to do this, except in very unusual circumstances. |
|
This page was last modified on 02 October, 2007 |