469,887 Members | 1,206 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

Static variable in method.

dmjpro
2,476 2GB
I m from C and C++ programming world.
There i saw that a static variable in a method lives between two function calls.
Right??
But in JAVA it is valid.
Look at this code carefully.....

Expand|Select|Wrap|Line Numbers
  1. class Sample
  2. {
  3.  //Some code
  4.  void test()
  5.  {
  6.   //Some code
  7.   static int a; //It is not valid ... the compiler error message flashes.
  8.   //Some code
  9.  }
  10.  //Some code
  11. }
  12.  
Plz explain ....

Kind regards,
Dmjpro.
Jun 5 '07 #1
4 1570
blazedaces
284 100+
I m from C and C++ programming world.
There i saw that a static variable in a method lives between two function calls.
Right??
But in JAVA it is valid.
Look at this code carefully.....

Expand|Select|Wrap|Line Numbers
  1. class Sample
  2. {
  3.  //Some code
  4.  void test()
  5.  {
  6.   //Some code
  7.   static int a; //It is not valid ... the compiler error message flashes.
  8.   //Some code
  9.  }
  10.  //Some code
  11. }
  12.  
Plz explain ....

Kind regards,
Dmjpro.
You can't create static variables in a non-static method. In fact, that's probably the EXACT error that showed up when you tried to compile this... am I wrong? What error did show up? I'm curious...

Also, the entire idea of a static variable: they're static, which means they are the same for all instances of that class, means that they should be created as instance variables. It only makes sense. You can set what they are in other methods, but you can't create them there.

Hope that helped,
-blazed
Jun 5 '07 #2
JosAH
11,448 Expert 8TB
Java doesn't implementt that feature. In C/C++ the lexical scope of a local static
variable is the function itself (it's invisible outside that function), but the variable
'lives' forever (as long as the process lives).

You can easily mimic functionally equivalent behaviour using a static class
variable. The variable can be 'seen' in every method of the class but if other
functions promise to stay away from it, the functionality is identical.

kind regards,

Jos
Jun 5 '07 #3
dmjpro
2,476 2GB
You can easily mimic functionally equivalent behaviour using a static class
variable. The variable can be 'seen' in every method of the class but if other
functions promise to stay away from it, the functionality is identical.
These lines r very tough to understand.
Plz tell in simple language.
Plz.

Kind regards,
Dmjpro.
Jun 6 '07 #4
JosAH
11,448 Expert 8TB
These lines r very tough to understand.
Plz tell in simple language.
Plz.

Kind regards,
Dmjpro.
This piece of code using C/C++:
Expand|Select|Wrap|Line Numbers
  1. int foo() {
  2.    static int bar= 0;
  3.    return bar++;
  4. }
Can be written in Java as follows:
Expand|Select|Wrap|Line Numbers
  1. private static bar= 0;
  2. public int foo() {
  3.    return bar++;
  4. }
Using the Java version the variable bar can be manipulated by other class and
instance methods; if they 'promise' to stay away from it the functionality of the
two code snippets is identical.

kind regards,

Jos
Jun 6 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by James | last post: by
6 posts views Thread by thomson | last post: by
44 posts views Thread by John A. Bailo | last post: by
37 posts views Thread by minkoo.seo | last post: by
reply views Thread by Joe Strout | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.