Consider the 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 this generates error even when i have already declared j above h .
The error is because you are mixing static declarations with instance variable declarations (which is pretty clear from the error message Cannot make a static reference to the non-static field j ). Change the first initialization to
static int j = 10;
and your code compiles just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.