Why can't i declare a static variable in a static method?

Z

ZelluX

for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks
 
V

visionset

ZelluX said:
Why can't i declare a static variable in a static method?

A local variable exists (or rather before eligible for GC) only for
the scope of the method, so the concept of static is not relevant.
 
C

Christian

ZelluX said:
for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks

java does not have this concept of a variable that doesn't change during
calls..

what you can do is declare the variable outside the method static..
(static final is java's version of a constant)

like
class Test {

private static final double PI = Math.PI ;

public static void main(String[] args) {
double radius = 5;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}
 
C

Chris Dollin

A local variable exists (or rather before eligible for GC) only for
the scope of the method, so the concept of static is not relevant.

There's nothing wrong with the idea of a static local variable, which
would be a static variable whose scope was restricted to the block
(hence method) in which it is declared. (Naturally an initialiser
for such a variable would not be allowed to refer to non-static locals.)

It's just that Java doesn't have them: I have no idea why.
 
C

Chris Dollin

Christian said:
ZelluX said:
for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks

java does not have this concept of a variable that doesn't change during
calls..

You can declare locals `final` -- doesn't that count?
 
C

Christian

Chris said:
Christian said:
ZelluX said:
for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks
java does not have this concept of a variable that doesn't change during
calls..

You can declare locals `final` -- doesn't that count?

nope ... doesn`t count ... the next call to the method the local will
have the value to which it is initialized to in the current call..
not the value to which it was initialized to with the first call.
 
C

Chris Dollin

Christian said:
Chris said:
Christian said:
ZelluX schrieb:
for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks
java does not have this concept of a variable that doesn't change during
calls..

You can declare locals `final` -- doesn't that count?
nope ... doesn`t count ... the next call to the method the local will
have the value to which it is initialized to in the current call..
not the value to which it was initialized to with the first call.

Indeed, but it fits your "variable that doesn't change during calls"
description, I thought.

I don't see that it's anything to do with /changes/ to the variable.
Java just doesn't allow statics that are scoped within a method.
It has a notion of variables that don't change during calls -- final
variables (local or not), and a notion of variables that do change
(non-final, local or not.)
 
L

Lew

Chris said:
Indeed, but it fits your "variable that doesn't change during calls"
description, I thought.

Except that it does change between calls.
I don't see that it's anything to do with /changes/ to the variable.
Java just doesn't allow statics that are scoped within a method.
It has a notion of variables that don't change during calls -- final
variables (local or not),

Except that a final local variable does change between calls.
and a notion of variables that do change (non-final, local or not.)

Except that it's a different final local variable with each call.
 
C

Chris Dollin

Lew said:
Except that it does change between calls.

I don't think so ...
Except that it's a different final local variable with each call.

.... and that's why; each call has it's own locals; but those locals
don't exist once the call terminates.
 
L

Lew

Chris said:
I don't think so ...


.... and that's why; each call has it's own locals; but those locals
don't exist once the call terminates.

And you don't regard a brand-new instance as a change?
 
C

Chris Dollin

Lew said:
Chris Dollin wrote:
(fx:snipping)


And you don't regard a brand-new instance as a change?

It's /a/ change, but it's not a change to "the" variable.
 
C

Christian

Chris said:
It's /a/ change, but it's not a change to "the" variable.

huh?


void foo() {
static Bar bar = new Bar(getCurrentTime());

bar.printTime(); //will print allways the same
}

java

void foo() {
final Bar bar = new Bar(getCurrentTime());

bar.printTime(); //will print allways the currentTime
}

so yes the variable is still called bar...
but what does not change beside this?
 
C

Chris Dollin

Christian said:
void foo() {
final Bar bar = new Bar(getCurrentTime());

bar.printTime(); //will print allways the currentTime
}

so yes the variable is still called bar...
but what does not change beside this?

Any given variable `bar` of `foo` has a single value that does not change.

It doesn't matter that there are arbitrarily many different variables all
called `bar` with different values at different times.

[Saying "/the/ variable", as above, is misleading, IMAO.]
 
K

Knute Johnson

Chris said:
Christian said:
void foo() {
final Bar bar = new Bar(getCurrentTime());

bar.printTime(); //will print allways the currentTime
}

so yes the variable is still called bar...
but what does not change beside this?

Any given variable `bar` of `foo` has a single value that does not change.

It doesn't matter that there are arbitrarily many different variables all
called `bar` with different values at different times.

[Saying "/the/ variable", as above, is misleading, IMAO.]

I hear static locals are coming. I'm not sure if they will be in 1.7 or
not but they have been discussed at some length here recently. There is
a name for them too, anybody remember what it is? CRS you know :).
 
R

Roedy Green

for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks

I have asked Sun to make that legal. It would give you a static
variable accessible only by one method. It would not even need a
change to the JVM, just the compiler. Perhaps you could submit an RFC.

See http://mindprod.com/jgloss/rfc.html

For now you must define a private static variable accessible to all
methods in the class.
 
J

Jim Korman

for example,
class Test {
public static void main(String[] args) {
double radius = 5;
static double PI = 3.15169;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}

I can't figure out why it is wrong
thanks

I have asked Sun to make that legal. It would give you a static
variable accessible only by one method. It would not even need a
change to the JVM, just the compiler. Perhaps you could submit an RFC.

See http://mindprod.com/jgloss/rfc.html

For now you must define a private static variable accessible to all
methods in the class.

I'm not sure I'm seeing how a method scoped static would be used.

Is the idea that the value would maintain its value between calls to
the method, but keep the scope local to the method? If so how
would you keep the declaration from overwriting the previously set
value?

Jim
 
R

Roedy Green

I'm not sure I'm seeing how a method scoped static would be used.

Is the idea that the value would maintain its value between calls to
the method, but keep the scope local to the method? If so how
would you keep the declaration from overwriting the previously set
value?

It would behave exactly like a private static. It would be
initialised to 0/null. However, it would be declared in a method, and
any references to it outside that method would be illegal, detected an
compile time.

You would use it for example in

void myMethod ()
{
static Random wheel;
// first use init.
if( wheel == null ) wheel = new Random();

for (int i=0;i<100;i++)
{
System.out.println( wheel.nextInt( 6 ) );
}
}

You are declaring that wheel is purely for the use of myMethod, but
other than that in is like a private static.

To allow one-time initialisation would get fairly hairy. That is why I
side-stepped the issue. Perhaps inability to init is a reasonably
easily defined way is why Sun did not allow this. It would be a wart
no matter how it was handled.

Another syntax would be something like this where you declare outside
the method.

private to myMethod static Random wheel = new Random();

you could also have private instance methods too:

private to myMethod Random wheel = new Random();
 
J

Jim Korman

It would behave exactly like a private static. It would be
initialised to 0/null. However, it would be declared in a method, and
any references to it outside that method would be illegal, detected an
compile time.

You would use it for example in

void myMethod ()
{
static Random wheel;
// first use init.
if( wheel == null ) wheel = new Random();

for (int i=0;i<100;i++)
{
System.out.println( wheel.nextInt( 6 ) );
}
}

You are declaring that wheel is purely for the use of myMethod, but
other than that in is like a private static.

To allow one-time initialisation would get fairly hairy. That is why I
side-stepped the issue. Perhaps inability to init is a reasonably
easily defined way is why Sun did not allow this. It would be a wart
no matter how it was handled.

Another syntax would be something like this where you declare outside
the method.

private to myMethod static Random wheel = new Random();

you could also have private instance methods too:

private to myMethod Random wheel = new Random();

Thanks for the discussion. The first time initialization was one of
the issues that had me questioning. Food for thought.

Jim
 
M

Mark Space

Roedy Green wrote:
o allow one-time initialisation would get fairly hairy. That is why I

One could just write

class C {
static void m() {
static int i = 10;
// ... etc. use i here
}
}

And have it understood that the assignment is only executed once, on
class load. It's a little hard to read, but not that much.
 
P

Patricia Shanahan

Mark said:
Roedy Green wrote:
o allow one-time initialisation would get fairly hairy. That is why I

One could just write

class C {
static void m() {
static int i = 10;
// ... etc. use i here
}
}

And have it understood that the assignment is only executed once, on
class load. It's a little hard to read, but not that much.

Maybe it would be better with a different syntax:

class C {
private {
static int i = 10;
void m() {
// ... etc. use i here
}
}
void n() {
// No access to i here.
}
}

That is, group a method and its method-local fields in a "private
block". I think this additional keyword use should be unambiguous - the
other uses of "private" are followed by a keyword or an identifier.

The variable would be declared static or not depending on whether it
should have a single value for all instances or not. The braces show the
scope limitation. The placement outside the method shows that it is a
field, not a local variable.

If the field is static, its initializer is executed on class load. If it
is non-static, its initializer is executed during object creation.

More generally, it is a normal private field declaration, with the
single exception that it can only be referenced in one method.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top