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

Discussion in 'Java' started by ZelluX, Nov 15, 2007.

  1. ZelluX

    ZelluX Guest

    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
    ZelluX, Nov 15, 2007
    #1
    1. Advertising

  2. ZelluX

    Guest

    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
    , Nov 15, 2007
    #2
    1. Advertising

  3. ZelluX

    Christian Guest

    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);
    }
    }
    Christian, Nov 15, 2007
    #3
  4. ZelluX

    Chris Dollin Guest

    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
    Chris Dollin, Nov 15, 2007
    #4
  5. ZelluX

    Chris Dollin Guest

    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
    Chris Dollin, Nov 15, 2007
    #5
  6. ZelluX

    Christian Guest

    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..
    Christian, Nov 15, 2007
    #6
  7. ZelluX

    Chris Dollin Guest

    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
    Chris Dollin, Nov 15, 2007
    #7
  8. ZelluX

    Lew Guest

    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
    Lew, Nov 15, 2007
    #8
  9. ZelluX

    Chris Dollin Guest

    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
    Chris Dollin, Nov 15, 2007
    #9
  10. ZelluX

    Lew Guest

    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
    Lew, Nov 15, 2007
    #10
  11. ZelluX

    Chris Dollin Guest

    Lew wrote:

    > Chris Dollin wrote:


    (fx:snipping)

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


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

    --
    Chris "existence is not a predicate, or something like that" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Nov 15, 2007
    #11
  12. ZelluX

    Christian Guest

    Chris Dollin schrieb:
    > Lew wrote:
    >
    >> Chris Dollin wrote:

    >
    > (fx:snipping)
    >
    >>> .... 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?

    >
    > 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?
    Christian, Nov 15, 2007
    #12
  13. ZelluX

    Chris Dollin Guest

    Christian wrote:

    > Chris Dollin schrieb:
    >> Lew wrote:
    >>
    >>> Chris Dollin wrote:

    >>
    >> (fx:snipping)
    >>
    >>>> .... 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?

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

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

    --
    Chris "singular and mutable" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, Nov 15, 2007
    #13
  14. Chris Dollin wrote:
    > Christian wrote:
    >
    >> Chris Dollin schrieb:
    >>> Lew wrote:
    >>>
    >>>> Chris Dollin wrote:
    >>> (fx:snipping)
    >>>
    >>>>> .... 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?
    >>> It's /a/ change, but it's not a change to "the" variable.

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

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Nov 15, 2007
    #14
  15. ZelluX

    Roedy Green Guest

    On Thu, 15 Nov 2007 02:12:18 -0800 (PST), ZelluX <>
    wrote, quoted or indirectly quoted someone who 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


    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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 15, 2007
    #15
  16. ZelluX

    Jim Korman Guest

    On Thu, 15 Nov 2007 23:08:37 GMT, Roedy Green
    <> wrote:

    >On Thu, 15 Nov 2007 02:12:18 -0800 (PST), ZelluX <>
    >wrote, quoted or indirectly quoted someone who 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

    >
    >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
    Jim Korman, Nov 16, 2007
    #16
  17. ZelluX

    Roedy Green Guest

    On Fri, 16 Nov 2007 03:45:58 GMT, Jim Korman <>
    wrote, quoted or indirectly quoted someone who said :

    >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();
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 16, 2007
    #17
  18. ZelluX

    Jim Korman Guest

    On Fri, 16 Nov 2007 05:12:59 GMT, Roedy Green
    <> wrote:

    >On Fri, 16 Nov 2007 03:45:58 GMT, Jim Korman <>
    >wrote, quoted or indirectly quoted someone who said :
    >
    >>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();


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

    Jim
    Jim Korman, Nov 17, 2007
    #18
  19. ZelluX

    Mark Space Guest

    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.
    Mark Space, Nov 18, 2007
    #19
  20. Mark Space wrote:
    > 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
    Patricia Shanahan, Nov 18, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    2
    Views:
    359
    jeffc
    Jun 15, 2004
  2. Markus Dehmann
    Replies:
    5
    Views:
    2,671
    red floyd
    Jul 1, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  4. CJ
    Replies:
    6
    Views:
    464
  5. Replies:
    1
    Views:
    378
    Victor Bazarov
    Apr 6, 2006
Loading...

Share This Page