开发者

A tricky question of simple Java: variable scope

开发者 https://www.devze.com 2023-03-14 15:14 出处:网络
I am not new to java and C#. I thought I understand the concept of variable scope until recently I was asked this question in an interview:

I am not new to java and C#. I thought I understand the concept of variable scope until recently I was asked this question in an interview:

public class Q{ //starting y scope

    static int x = 11;
    private int y = 33; // Just added a “private” modifier to make it clearer.

    public static void m开发者_StackOverflowain(String args[]){
        Q q = new Q();
        q.call(5);
    }

    public void call(int x){
        Q q = new Q();

        this.x = 22;
        y = 44;

        System.out.println("Output: " + Q.x);
        System.out.println("Output: " + q.x);
        System.out.println("Output: " + q.y);
    }

} //ending y scope

Define the output of this program.

I answered the question during the interview that the output would be a runtime exception. To my understanding, y is declared private, and the instance method call() is trying to access the instance private variable y of another instance of class Q. How could that happen at all!? However answering this question wrongly didn't affect my interview too much because this is the kind of "tricky basic" question. But, answering wrongly means my years' Java experience needs rehab, that's terrible!

Could someone help me on this matter? I would be so much appreciated!


You can access private members of your own class, even from a different instance of the class.


private just means it can't be accessed by objects of another class, not any other objects. So one Q object can access the private members of another Q object.

[Note that if this were illegal, this would trigger a compiler error, not a runtime exception.]


Private variables are accessible within the program text of the declaring class. It doesn't matter if one instance tries to access variables in another instance, so long as the code is within the same class.

From the JLS section 6.6:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.


private means that classes outside of this class cannot see the variable. Within this class definition, all methods and variables can be accessed.


Access restrictions should be enforced compile time not in run time, so the compiler should prevent compiling the code if it was wrong.

But an access modifier is applied on class level, so you can always access a private member from the class it self.

When you clone an object this is often useful since you can just set a bunch of values in the clone directly, which might have been hard to access otherwise. Also when comparing objects with an Equals method it can be quite handy that it is the class not the instance that defines the access restrictions.


As it was already pointed out, private members of the other instances are accessible from the class. Also, you were incorrect about the runtime exception: there will be no runtime exception even if you modify the code so that you try to access a private member of another class, the code will just fail to compile.


I think there will be a compilation problem because the attribute static int x = 11 is declared static and in the method call, the variable x is accessed by a this. You cannot make a this to a static variable in a class.

There is no problem with the private member y.


The correct "interview" answer is that whoever wrote code like this should be fired! Lousy class name, useless comments about y scope; non-standard use of "this" which should really only be used for instance variables, not class variables; re-using 'q' in the main and call methods.

It reminds me of the obfuscated C++ contests, exactly the sort of nonsense Java was designed to avoid.

Worse than this, though, is that it's very difficult to tell from the comments and the answers what the output of the program should be. Do I go by the '6' on the comment by Kevin?

I wish an admin would refactor all this. And I would tell an interviewer (if this really is an interview question, which I doubt) that we should run the code, get the answers, and then create a simple test to confirm that the answers don't change when we refactor the code to conform to standard usage.

0

精彩评论

暂无评论...
验证码 换一张
取 消