+ 1

What is error in this code

class Test { private int n = 10; static { n = 11; } public Test(){ n = 12; } public void print(){ System.out.println("n="+n); } public static void main(String args[]){ Test t = new Test(); t.print(); } }

15th Jan 2020, 6:32 AM
Mukul
Mukul - avatar
1 Answer
+ 2
From the static content, you can not access the non-static variables, methods... static { n=11; } // non-static variable accessing from static block, hence error. To access n, you need like this.... private static int n=10; static { n=11; }
15th Jan 2020, 11:49 AM
Jayakrishna 🇼🇳