Hi,
According to K&B,
class Frog {
static int frogCount = 0; // Declare and initialize static variable
public Frog() {
frogCount += 1; // Modify the value in the constructor
}
}
class TestFrog {
public static void main (String [] args) {
new Frog();
new Frog();
new Frog();
System.out.print("frogCount:"+Frog.frogCount); //Access static variable
}
}
The book says that,we can access Static Member as follow:
Frog f = new Frog();
int frogs = f.getFrogCount;
ans explanation is given that:This is merely a syntax trick to let you use an object reference
variable (but not the object it refers to) to get to a static method or variable, but the
static member is still unaware of the particular instance used to invoke the static
member. In the Frog example, the compiler knows that the reference variable f is of
type Frog, and so the Frog class static method is run with no awareness or concern
for the Frog instance at the other end of the f reference.
My doubt is (according to above statement) i cannot access the getFrogCount as:
Frog f;
int frogs = f.getFrogCount;
This gives me a compiler error:the local object f is may not have been intialise.I feel that as getFrogCount is static ,we don't want it to be intialised.
Thanks in advance