George Marin
George Marin

Reputation: 395

Main and objects,Java

I have a noob questions,s orry for wasting your time but could somebody explain?

public class Ex {
    public  int a = 0;
    public Ex(int x){
            a = x;
        int y = 10;
    }


    public static void main(String[] args){
        Ex obj = new Ex(10);
        System.out.println(Ex.a);   
    }
}

Why isn't this working? I tried to put constructor in main but that ain't working either.

Upvotes: 0

Views: 43

Answers (2)

Joe Taras
Joe Taras

Reputation: 15379

Try this:

System.out.println(obj.a);

Ex.a you can use if your attribute is static, but a is not static

Upvotes: 2

Eran
Eran

Reputation: 393781

Your object is called obj, and a belongs to your object, not to your class.

System.out.println(obj.a);   

Ex.a would work if a was a static class member.

Upvotes: 0

Related Questions