class - Static variable initialization in java -


consider following code :

  public class testclass      {       int j=10;       static int h=j;        testclass()       {            system.out.println(h);       }        public static void main(string[] args)       {           testclass obj= new testclass();                }      } 

why generates error when have declared j above h .

the error because mixing static declarations instance variable declarations (which pretty clear error message cannot make static reference non-static field j). change first initialization

static int j = 10; 

and code compiles fine.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -