Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access to local variable in inner class

Tags:

java

package geometry;

public class Hypotenuse {
    public InnerTriangle it = new InnerTriangle();

    class InnerTriangle {  
        public int base;
        public int height;
    }
}

Which statement is true about the class of an object that can reference the variable base?

A. It can be any class.

B. No class has access to base.

C. The class must belong to the geometry package.

D. The class must be a subclass of the class Hypotenuse

This is from SCJP Dumps, Answer is "C". As my knowledge answer should be "B" because inner class has local variable called "base" and it has scope only in the inner class. Even if i want to usethis variable in "geometry" class i am not allowed to do it.

Please guide me if i am wrong?

like image 526
user2985842 Avatar asked Feb 13 '14 16:02

user2985842


1 Answers

The class InnerTriangle has a "package level" scope since you have not specified any explicit access modifier. This means members in the class as well as members in the package are allowed access to the class.

That is why "C" is the right answer.

like image 183
VeeJay Avatar answered Sep 28 '22 18:09

VeeJay