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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Ambiorix
Posts: 25
Joined: Thu Jan 10, 2013 8:45 am
Contact:

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

Post 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'

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest