non static variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay123456
    New Member
    • Sep 2006
    • 125

    #1

    non static variable

    Dear Friends
    Code:
    public class hello
    {
              int i=0;
              public static void main(String args[])
              {
                    System.out.print(i);
                }
    }
    can it possible that i can use int i in main function without declaration as static ?

    sanjay
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you either make it static or you access it via an object of type hello
    Code:
    public class hello
    {
              int i=0;
              int geti() { return i; }
              public static void main(String args[])
              {
                    hello h=new hello();
                    System.out.print(h.geti());
                }
    }
    and each hello object will have its own copy of i

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Find time to read this.

      Comment

      • sanjay123456
        New Member
        • Sep 2006
        • 125

        #4
        Thx dear

        i have a problem that '

        why java accept a varoable as static in only class level

        we did not declare a variable as static in function level ?

        sanjay

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by sanjay123456
          Thx dear



          i have a problem that \'



          why java accept a varoable as static in only class level



          we did not declare a variable as static in function level ?



          sanjay


          If a method is static, all the variables declared in that method are implicitly static. Also you cannot declare static variables inside non-static methods.

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by sanjay123456
            Thx dear

            i have a problem that '

            why java accept a varoable as static in only class level

            we did not declare a variable as static in function level ?

            sanjay
            are you thinking of static local variables in C functions, e.g.
            Code:
            int test()
            {
               static int x=0;
               x++;
              return x;
            }
            x is initialised to 0 in the first function call and then incremented on sucessive calls - it retains its value between calls unlike 'normal' local variables the value of which is lost and reinitialised on each call (if it has an inital value otherwise it is undefined), Sucessive calls of test() return 1, 2, 3, etc.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              static variables are those variables which can be accessed without instantiate the corresponding class.
              here the main class u used jvm didn't instantiate..
              the static members of a class are used only in static function of that class.

              Comment

              Working...