
public class Add{ int foo=7; int bar=8; public int add(int x,int y){ int z=x+y; System.out.println(z); return z; }//add public static void main(String []s){ Add a1=new Add(); a1.add(7,8); Add a2=new Add(); a2.add(foo,bar); }//main }//class
I wrote the following program. When I compiled this I got the error message saying "non static variable cannot be referenced from static context". How to solve this problem?

public class Add{
static int foo=7;
static int bar=8;
public int add(int x,int y){
int z=x+y;
System.out.println(z);
return z;
}
public static void main(String []s){
Add a1=new Add();
a1.add(7,8);
Add a2=new Add();
a2.add(foo,bar);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.