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;       

Edited 4 Years Ago by pyTony: not tested code snippet, moved to normal discussion

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.

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.

This question has already been answered. Start a new discussion instead.