wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing a Static Member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing a Static Member" Watch "Accessing a Static Member" New topic
Author

Accessing a Static Member

Apna Apana
Greenhorn

Joined: May 01, 2007
Posts: 20
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
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
just add line 2 in your code

Frog f;
f = new Frog();
int frogs = f.getFrogCount;


Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
Apna Apana
Greenhorn

Joined: May 01, 2007
Posts: 20
Hi,
Actually my doubt is when we create a reference means why don't we call the static members of the class referenced i.e., of class type(as K&B said)
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Well,

The static members can be accessed in two ways,

1) Directly with the name of the class. //Code 2 shows it.

2) Normally by using the instance of the class //code 1 shows it.




Regards,
Durga Prasad
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi Apna,

try the following:
Apna Apana
Greenhorn

Joined: May 01, 2007
Posts: 20
Hi Manfred Klug,
Ya i got it but what is the difference between
Frog f;
Frog f=null;

Thanks in advance
suhas Koley
Greenhorn

Joined: May 24, 2007
Posts: 11
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();


Frog f ;
f= new Frog();

System.out.print("frogCount:"+Frog.frogCount); //Access static variable

System.out.print("frogCount:"+f.getfrogCount);
//You use the variables (getfrogcount) but there is no such static variable (getfrogcount) here

//use thisSystem.out.print("frogCount:"+f.frogCount);


}
}


PROFILE:<br />Suhas koley <br />email:suhas.koley@gmail.com<br />College:NIT(REC)Durgapur
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Apna Apana:
Ya i got it but what is the difference between
Frog f;
Frog f=null;


'Frog f=null' declares and initializes the variable, whereas 'Frog f' only declares it.
ramesh kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 25
Local variables has to be initialized before they used.So here compiler is stoping you from using local variables to access frogCount.
Apna Apana
Greenhorn

Joined: May 01, 2007
Posts: 20
Hi Ramesh,
I forget that point.Anyhow thank you.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
My thinking is, you can refer to frogCount one of two ways, either directly by the class name

int i = Frog.frogCount;

or, through an instance of the Frog class

Frog frog1 = new Frog;
int i = frog1.frogCount;

Frog frog2;
int i = frog2.frogCount will not work because a reference variable, frog2, has been declared but no class has been instantiated.


------------------------
Bob
SCJP - 86% - June 11, 2009
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Bob Ruth:
Frog frog2;
int i = frog2.frogCount will not work because a reference variable, frog2, has been declared but no class has been instantiated.


Bob,

As someone pointed out earlier, try:



SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
 
Consider Paul's rocket mass heater.
 
subject: Accessing a Static Member
 
Similar Threads
Accessing Static Member Using Object Reference Variable
accessing static members via reference variables
Accessing Static Variable
instance on static method
Regarding reference variable