Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1343 :

Posted: Sun Oct 28, 2012 8:50 pm
by ETS User
With NetBeans 7.3 Beta:

The following produces, "non-static variable this cannot be referenced from a static context".

Code: Select all

public class TestClass {
    int x;
    public static void main(String[] args) {
        System.out.println(this.x);
    }
}
The following produces, "non-static variable this cannot be referenced from a static context - Accessing static field x".

Code: Select all

public class TestClass {
    static int x;
    public static void main(String[] args) {
        System.out.println(this.x);
    }
}
NetBeans refactoring then produces this:

System.out.println(TestClass.x);

Please advise.

Re: About Question enthuware.ocajp.i.v7.2.1343 :

Posted: Mon Oct 29, 2012 7:41 am
by admin
That is why the option "main cannot access this.x as it is declared now i.e. without any modifier." is the correct option.
I am not sure what is your doubt. Can you please explain a bit more so that I can help?
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1343 :

Posted: Wed Feb 13, 2013 4:09 am
by Ambiorix
I'm not happy with any of the options.

The given correct answer

"main cannot access this.x as it is declared now i.e. without any modifier"

implies that, with the addition of a modifier, main could access this.x.

By making x static, main can access x but it still can't say 'this.x'

Re: About Question enthuware.ocajp.i.v7.2.1343 :

Posted: Wed Feb 13, 2013 8:13 am
by admin
Ambiorix wrote:I'm not happy with any of the options.

The given correct answer

"main cannot access this.x as it is declared now i.e. without any modifier"

implies that, with the addition of a modifier, main could access this.x.

By making x static, main can access x but it still can't say 'this.x'
The part "i.e. without any modifier" is only a statement of the fact because that is how x has been declared as of now. It has been removed to avoid any confusion.

thank you for your feedback!