CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Simple Question

  1. #1
    Join Date
    Aug 1999
    Location
    Pune, India
    Posts
    8

    Simple Question

    What's wrong in following code,

    class temp {
    public static void main(String args[]) {
    static int i=10;
    }
    }

    Why can not i have a static variable in a static function?







  2. #2
    Guest

    Re: Simple Question

    Static variables are associated with a class and not methods.A static variable is a single copy for a class and can be accessed using class name.A variable declared in a method has scope limited to the function and how is it possible to access a variable in a method using method name .I think this type of declaration hold good in C and not in Java(OOPS languages).


  3. #3
    Guest

    Re: Simple Question

    Why not just make the static variable a class variable (move it to the class declaration)? The effect will be the same, and it will be in the same namespace.

    So you would have:

    class temp {
    private static int i=10;

    public static void main(String args[]) {
    }
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured