Hey guys,

I'm a starter so I had a doubt related to placement of a static variable. On line 5, for "static int d=5" compiler issues "illegal start of expression". But when I place it outside main() as Instance variable it works and prints "Hello 5". Why this thing?

  public class AbstractClass {      

  public static void main(String args[]) {      
      String s = "Hello";       
      static int d=5;       
      System.out.println(s);        
      System.out.println(d);
      } 
  }

hi rahul.ch ,
We cant declare static varibale as local method variable
because static variables are belongs to class not object ,we can acess the staic variable using class name directly, When you declare a variable inside method(staic or non static) it is local varible we could not access out side the class.

Thanks a lot anand. Appreciate the clarity.

actually, we can't declare a local variable as being static :) (potato, potatoe)
you have three options: either you have it as a local (non-static) variable that is defined within the main method, or you have it on class scope, either static or non-static.

Thanks stultuske :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.