Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why can't i declare a static variable in a static method?

Reply
Thread Tools

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

 
 
ZelluX
Guest
Posts: n/a
 
      11-15-2007
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
 
Reply With Quote
 
 
 
 
visionset@yahoo.com
Guest
Posts: n/a
 
      11-15-2007

ZelluX wrote:

> 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.

--
mike w
 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      11-15-2007
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..

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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-15-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> ZelluX wrote:
>
>> 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.


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.

--
Chris "perhaps method calls generate friction => sparks?" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-15-2007
Christian wrote:

> 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?

--
Chris "then ZZ1, ZZ2 ZZ3" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      11-15-2007
Chris Dollin schrieb:
> Christian wrote:
>
>> 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.

--
java only has global variables for this.. there are no global variable
with scope to one method..
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-15-2007
Christian wrote:

> Chris Dollin schrieb:
>> Christian wrote:
>>
>>> 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.)

--
Chris "disentangling" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-15-2007
Chris Dollin schrieb:
>>> You can declare locals `final` -- doesn't that count?


Christian wrote:
>> 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.


Chris Dollin wrote:
> 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.

--
Lew
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-15-2007
Lew wrote:

> Chris Dollin schrieb:
>>>> You can declare locals `final` -- doesn't that count?

>
> Christian wrote:
>>> 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.

>
> Chris Dollin wrote:
>> 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 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.

--
Chris "can non-existant things change?" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-15-2007
Chris Dollin wrote:
> Lew wrote:
>
>> Chris Dollin schrieb:
>>>>> You can declare locals `final` -- doesn't that count?

>> Christian wrote:
>>>> 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.

>> Chris Dollin wrote:
>>> 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 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.


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

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why connot declare a static member of STRUCT or UNION? arkmancn@gmail.com C Programming 37 10-27-2007 08:30 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why can't we declare an argv variable? CJ C Programming 6 06-09-2006 02:01 PM
static variable:declare and define linq936@hotmail.com C++ 1 04-06-2006 05:31 PM



Advertisments