The problem is that you set the value of a and b after you've created a new instance of Main, i.e. after the constructor is executed.
You should pass in the values of a and b to the constructor:
public class Main
{
int a;
int b;
int c;
public Main(int _a, int _b)
{
a = _a;
b = _b;
c = a + b;
}
void display()
{
System.out.println(c);
}
public static void main(String args[])
{
Main m = new Main(10, 50);
m.display();
}
}
Note: I do not work with Java and it surprised me that you can define a constructor just by opening a new scope in the class without specifying the function name. Wow.