java question about static variable in static method

Discussion in 'Java' started by Aakash, Jan 18, 2008.

  1. Aakash

    Aakash Guest

    HI, I have a simple program which does not compile:
    public class class1{

    public static void main(String[] args)
    {
    static int i=0;//static is not allowed, why?
    }

    }

    Can you tell me why this is not allowed?
    Thanks
    Aakash
    Aakash, Jan 18, 2008
    #1
    1. Advertising

  2. Aakash wrote:
    > HI, I have a simple program which does not compile:
    > public class class1{
    >
    > public static void main(String[] args)
    > {
    > static int i=0;//static is not allowed, why?
    > }
    >
    > }
    >
    > Can you tell me why this is not allowed?
    > Thanks
    > Aakash


    Static variables may only be class variables. There is some discussion
    about 'closures', which are supposed to be like static variable, being
    added in version 7.

    --

    Knute Johnson
    email s/nospam/knute/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jan 19, 2008
    #2
    1. Advertising

  3. Aakash <> writes:

    > HI, I have a simple program which does not compile:

    ....
    > public static void main(String[] args)
    > {
    > static int i=0;//static is not allowed, why?
    > }

    ....
    > Can you tell me why this is not allowed?


    Because it is not part of the Java syntax.

    More relevantly: What would you expect it to do?

    If you want something like static local variables in the C language,
    it does not exist in Java. Instead use a private field on the object
    that the method belongs to, that restricts access to methods in the
    class instead of just the one method, but it should solve the same
    problems that the C static local variable does.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 19, 2008
    #3
  4. Aakash

    Roedy Green Guest

    On Fri, 18 Jan 2008 15:11:39 -0800 (PST), Aakash <>
    wrote, quoted or indirectly quoted someone who said :

    >public static void main(String[] args)
    >{
    > static int i=0;//static is not allowed, why?
    >}
    >
    >}
    >
    >Can you tell me why this is not allowed?


    This is Java, not C++. You must declare statics outside methods. They
    belong to the class, not to a particular method. I think the reason
    it was not is initialisation. Does it get inited every method
    execution, just the first time? In what context? It was too messy to
    define that, so they disallowed it.

    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
    Roedy Green, Jan 19, 2008
    #4
    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. G. Burton
    Replies:
    2
    Views:
    59,885
    Sudsy
    Feb 21, 2004
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,827
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,415
    Denis Perelyubskiy
    Sep 5, 2003
  4. ZelluX
    Replies:
    24
    Views:
    882
    Hendrik Maryns
    Nov 19, 2007
  5. Stefan Istrate

    Static variable & static method

    Stefan Istrate, Jun 6, 2008, in forum: C++
    Replies:
    8
    Views:
    356
    James Kanze
    Jun 7, 2008
Loading...

Share This Page