Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a Static Member

 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just add line 2 in your code

Frog f;
f = new Frog();
int frogs = f.getFrogCount;
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apna,

try the following:
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred Klug,
Ya i got it but what is the difference between
Frog f;
Frog f=null;

Thanks in advance
 
suhas Koley
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);


}
}
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramesh,
I forget that point.Anyhow thank you.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic