Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Static Variable Code question..

Ranch Hand
Posts: 74
I want to know the output for this program. Its spitting 10, I am testing on the application of static variables in Kathys book.

Thanks
[ October 15, 2004: Message edited by: Barry Gaunt ]

Ranch Hand
Posts: 74
oops!! I forgot to mention, why is it showing the value 10? I Debugged the application but couldnt convince myself..anythoughts on this are welcome.

Barry Gaunt
Ranch Hand
Posts: 7729
Which frogCount is it printing? And what kind of variable is that frogCount?
By the way, I do not mean the type (int) of the variable.
[ October 15, 2004: Message edited by: Barry Gaunt ]

Krishna Srinivasan
Ranch Hand
Posts: 1874
The output will be 10,10.
bcos you are pritning the local variables.

Barry Gaunt
Ranch Hand
Posts: 7729
Originally posted by Krishna Srinivasan:
The output will be 10,10.

Nope. It prints 10.
[ October 15, 2004: Message edited by: Barry Gaunt ]

Arnab karmakar
Ranch Hand
Posts: 46

There is nothing complicated. The output will be 10 only.
If u r thinking of why the static varible frogCount is taken into consideration ... this is becoz if u want to change that one u will have to use this.frogCount.

I think this helps.

Arnab

kapil munjal
Ranch Hand
Posts: 298
Hi Friends,

I was trying this question and I came out with this thing which I would like to share.

I did some modification in the program:

=========================================================
public class Teststatic {
static int frogCount=0;
public Teststatic(int frogCount){
frogCount+=1;
}
public static void main(String[] args) {
int frogCount=10;
new Teststatic(frogCount);
new Teststatic(frogCount);

System.out.println("Teststatic.frogCount "+Teststatic.frogCount);
System.out.println(frogCount);
}
}
=========================================================

When I run this program, the output is
Teststatic.frogCount 0
10

Well, still the static frogCount is not printing one or two because of the simple rule of "PASS BY VALUE".

We are just sending 10 to the constructor and nothing else. and it increases that value and 10 and finishes. We are doing it two times but still there is no change anywhere else in the program.

So here comes another thing we need to understand is "SCOPE AND LIFETIME OF VARIABLE".

so we have 3 different frogCount....

1. Member (static int frogCount=0
2. Inside constructor(int frogCount)

[Please tell me if a variable inside constructor is also called an Automatic Variable or not???]

3. Automatic variable (int frogCount)

Kaps
[ October 16, 2004: Message edited by: kapil munjal ]

Barry Gaunt
Ranch Hand
Posts: 7729
Kaps is close to being correct in saying we have three frogCount variables:
• static int frogCount - this is a member variable specific to the class. That is, TestStatic.frogCount is a class member variable
• TestStatic(int frogCount) - this is a variable local to the constructor
• int frogCount in the static main method - this is a variable local to the main method.

• There is no instance member variable called frogCount. By this I mean a member specific to an object (or instance) of the class TestStatic.

(Edited it clean up terminology)
[ October 16, 2004: Message edited by: Barry Gaunt ]

kapil munjal
Ranch Hand
Posts: 298
Thanks Barry,

You cleared one more concept here. I always thought that static variables can also be called member variables but they are not.

Static variable are always specific to the class.

Can we call them 'Class variables' also???

Kaps

Barry Gaunt
Ranch Hand
Posts: 7729
Originally posted by kapil munjal:
Thanks Barry,

You cleared one more concept here. I always thought that static variables can also be called member variables but they are not.

Static variable are always specific to the class.

Can we call them 'Class variables' also???

Kaps

Kaps, I edited my last post to be a little more clearer. There are
• class member variables (static variables) specific to the class.
• instance member variables specific to each object created from the class.
• local variables which are defined in and specific to methods, or are the formal parameters to a method.

• [ October 16, 2004: Message edited by: Barry Gaunt ]

kapil munjal
Ranch Hand
Posts: 298
Thanks again Barry,

I just needed to know the types of member variables, I think I have not read such type of explanation about static, member, instance variables earlier. So, I was a little confused about this.

Well, now after editing your last post, I know what exactly are these variables.

Thanks
Kaps