Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

static variable in Java

1 view
Skip to first unread message

Madhumita Mallik

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to

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;
}
}

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
_________________________________________________________________________

Derek T Lac

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to

Madhumita Mallik (mi...@unx.dec.com) wrote:
:
: Class A {

: public void A() {..}
:
: public void foo() {
: static B a = null;
:
: if(a!=null)
: a = new B;
: }
}

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.


Tov Are

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to

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.

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]


Bill Wilkinson

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to Madhumita Mallik

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.

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" );
}
}

Uday Pai

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to Madhumita Mallik

This should work, I guess:

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;
> }
> }
>

John Lacey

unread,
Dec 10, 1997, 3:00:00 AM12/10/97
to

> Madhumita Mallik (mi...@unx.dec.com) wrote:
> :
> : Class A {
> : public void foo() {

> : static B a = null;
> : // [...]
> : }
> : }

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

Amol

unread,
Dec 11, 1997, 3:00:00 AM12/11/97
to

Hi! It seems to me that you are planning on storing a value
computed/changed in foo(), which is an instance method. Thus, the value can
be considered an attribute of the instance and can possibly be stored in a
member variable.

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 Briggs

unread,
Dec 19, 1997, 3:00:00 AM12/19/97
to Madhumita Mallik

Define the variable statically at the global level???

Bernard

David Graff

unread,
Dec 20, 1997, 3:00:00 AM12/20/97
to

There is no such thing as a 'global' level in Java. What you'd do is
the following:

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;

0 new messages