Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Variable Code question.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Variable Code question.." Watch "Static Variable Code question.." New topic
Author

Static Variable Code question..

Madhu Kumar Vatts
Ranch Hand

Joined: Apr 01, 2004
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
Madhu
[ October 15, 2004: Message edited by: Barry Gaunt ]

Oracle Certified Enterprise Architect (Part 1);SUN Certified Programmer;SUN Certified Business Component Developer;SUN Certified Web Component Developer;
Oracle Certified WebServices Developer;Oracle Certified Java Persistence API Developer;Oracle Certified SQL Expert;IBM Certified XML Solution Developer
Madhu Kumar Vatts
Ranch Hand

Joined: Apr 01, 2004
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.
Madhu
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

The output will be 10,10.
bcos you are pritning the local variables.


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

Joined: Oct 04, 2004
Posts: 46
Hi Madhu,

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

Joined: May 11, 2004
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 ]

Kapil Munjal
SCJP 1.4, SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

    Joined: May 11, 2004
    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

    Joined: Aug 03, 2002
    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

    Joined: May 11, 2004
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Static Variable Code question..
     
    Similar Threads
    Accessing a Static Member
    static objects
    Accessing Static Member Using Object Reference Variable
    static variables are not accesed by class instances????
    Accessing Static Variable