as far as i know we can declare static local variables in C++,
cannot we do the same thing in java?
Hi
Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.
Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.
Thanks
Anand Iyer
He is coming from c & c++ background that's why he is asking such a Question.
Let him understand the Java Framework :-)
as far as i know we can declare static local variables in C++,
cannot we do the same thing in java?
Why don't you try it on the compiler and see what happens?
There's also a few tutorials around (even on this forum) that explain the static keyword in Java.
Whenever you declare a variable it becomes accessible across a class.
That's only true, if this variable was declared globally. If you have something like this
[CODE=java]
public void myMethod()
{
int var = 3;
}
[/CODE]you won't be able to access var outside of that method.
Originally posted by Anand Iyer
I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.
I think what is meant is something like this:
[CODE=java]
public void myMethod()
{
static int var = 3; // This is not allowed in Java
var++;
System.out.prin tln(var);
}
[/CODE]where the output would be:
4
5
6
7
when called 4 times. But no, it certainly can't be done in that way and I don't think it can be done at all in Java.
Comment