I would like to do something like this:
Class A {
public void A() {..}
public void foo() {
static B a = null;
if(a!=null)
a = new B;
}
}
Thanks,
Mita
--
_________________________________________________________________________
Madhumita Mallik TEL:(908) 577-6028
Software Development Engr. EMAIL:mi...@unx.dec.com
Digital Equipment Corp.
200 Route 9 North
Manalapan, NJ-07726-6000
_________________________________________________________________________
Why wouldn't that work? That should work in Java. In fact, if you delete
A and allocate a new instance of A. That value should be retained.
example:
class MyCounter {
int c;
void increment() {
c++;
}
int getCounter() {
return c;
}
}
Madhumita Mallik wrote in message <348EE8...@unx.dec.com>...
>How do I do it in Java as there is no concept of static variables
>at function scope.
[cut]
Put the static variable at class scope and make it private.
True, other methods in that class can get to it, but if
it's your class and you're silly enough to do that, you
deserve what you get.
class C
{
private static int counter = 0;
public void method( )
{
System.out.println( "method called "
+ ( ++counter ) + " times" );
}
}
class A {
public static B a = null;
public void A() {..}
public void foo() {
if (A.a!=null)
a = new B;
}
}
I havent tried compiling this. I am not +ve if this should work. Just
a suggestion.
Uday
ud...@ionet.net
Madhumita Mallik wrote:
> Hi,
> I have a function in java, where I would like a variable to
> retain its previous value it was assigned to. Normally in C I would
> declare this
> as a static variable inside the function.
> How do I do it in Java as there is no concept of static variables
> at function scope.
>
> I would like to do something like this:
>
> Class A {
> public void A() {..}
>
> public void foo() {
> static B a = null;
>
> if(a!=null)
> a = new B;
> }
> }
>
Derek T Lac wrote:
>
> Why wouldn't that work? That should work in Java.
No, Java doesn't support this. There is a sentence in The Java
Programming Language hinting that it does, but the Language
Specification doesn't describe them anyplace.
Tov Are wrote:
>
> In java you would declare a normal variable in your class body. This
> is really a better solution than what you have been using in C.
While this is an interesting case, a class field is closer to the
same semantics, as in Bill Wilkinson's post. As Bill points out,
the scoping isn't perfect.
A good rule is to put declarations in the smallest scope
possible. In C++, I use the static variable inside the function,
but in Java you can't. Often I *do* want a per-object field,
scoping within a function, but neither Java nor C++ provides
that.
John L
Y/N ? Am I missing something ?
Cheers.
-- Amol.
(am...@bigfoot.com)
Madhumita Mallik <mi...@unx.dec.com> wrote in article
<348EE8...@unx.dec.com>...
> Hi,
> I have a function in java, where I would like a variable to
> retain its previous value it was assigned to. Normally in C I would
> declare this
> as a static variable inside the function.
> How do I do it in Java as there is no concept of static variables
> at function scope.
>
> I would like to do something like this:
>
>
> Class A {
> public void A() {..}
>
> public void foo() {
> static B a = null;
>
Bernard
Class A {
public void A() {..}
public void foo() {
//if(a!=null) <= I'm assuming the next line is what
// <= you realy meant
if(a == null)
a = new B;
}
//Private Static Class Member
//This is assuming that you have class B defined along your
//class path somewhere.
private static B a = null;