Java Tutorial 20: (part 2 of 5)Nested Classes: Static member classes in Java (AKA top level nested classes). See next tutorial for other nested classes.
If class Test extends Top.Nest1, does it also inherit all of class Top members?
I'm looking at the line "int x = (new Top()).j;", and I have trouble understanding it, the only explanation would be that the it does inherit all of the above.
I ask this because "j" is marked as private in class Top, and it is being accessed from outside the Top class.
You're right both tp.j and (new Top()).j will fail - I'm trying to show too much on one diagram. If j wasn't private it would work. I made it private so I could show access to private fields in Nest1.
Yes. When you're in a class there are 2 direction you can go to look for a member. Up through the inheritance tree or outwards through enclosing classes. (You can't go up a bit and then out).
Originally I had static int k; in Top, but I took it out so I could put in more text and changed k in Test to be j :-( The thing I really wanted to show in Test, was the use of a null reference to get a static field doesn't give a null pointer exception - that's surprising.
If class Test extends Top.Nest1, does it also inherit all of class Top members?
I'm looking at the line "int x = (new Top()).j;", and I have trouble understanding it, the only explanation would be that the it does inherit all of the above.
I ask this because "j" is marked as private in class Top, and it is being accessed from outside the Top class.
Thank you!
liva236 1 year ago
You're right both tp.j and (new Top()).j will fail - I'm trying to show too much on one diagram. If j wasn't private it would work. I made it private so I could show access to private fields in Nest1.
Zantorc 1 year ago
Thank you!
So, the conclusion would be that when class Test extends Top.Nest1, it doesn't inherit all of class Top members, only Nest1's?
liva236 1 year ago
Yes. When you're in a class there are 2 direction you can go to look for a member. Up through the inheritance tree or outwards through enclosing classes. (You can't go up a bit and then out).
Originally I had static int k; in Top, but I took it out so I could put in more text and changed k in Test to be j :-( The thing I really wanted to show in Test, was the use of a null reference to get a static field doesn't give a null pointer exception - that's surprising.
Zantorc 1 year ago