Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Accessing a Static Member

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Apna,

try the following:
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramesh,
I forget that point.Anyhow thank you.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic