Author Topic:   What does Mr.Mocus mean by this ?
maha anna
bartender
posted February 26, 2000 09:52 PM             
I came across this question in Morcus tutorial.
For sure option 2 is true(There is a typo there).For sure options 1 and 3 are wrong.
What about 4th option? I thought it is true. Because a class defined inside a method can access all local final vars defined inside the method + all final args to that method + all vars which are accessible to the enclosing method
But Morcus says ans as 1. Can anybody clarify this. I tested this 4th option with some sample code also. Am i missing something here? Can anybody clarify this?
Thank you
maha anna

Which of the following statements are true?
1) A class defined within a method can only access static methods of the enclosing method
2) A class defined within a method can only access final methods of the enclosing method
3) A class defined with a method cannot access any of the fields of the enclosing method
4) A class defined within a method can access any fields accessible by the enclosing method

[This message has been edited by maha anna (edited February 26, 2000).]

Samer Salah
greenhorn
posted February 27, 2000 12:33 AM             
Hi,
I think the right answer is number 2
the classes that are defined within method can only access final variables from the enclosing method, that's because these variables will not be available for the class after the method exited, so they must be final.
quote:
Originally posted by maha anna:
I came across this question in Morcus tutorial.
For sure option 2 is true(There is a typo there).For sure options 1 and 3 are wrong.
What about 4th option? I thought it is true. Because a class defined inside a method can access all local final vars defined inside the method + all final args to that method + all vars which are accessible to the enclosing method
But Morcus says ans as 1. Can anybody clarify this. I tested this 4th option with some sample code also. Am i missing something here? Can anybody clarify this?
Thank you
maha anna

Which of the following statements are true?
1) A class defined within a method can only access static methods of the enclosing method
2) A class defined within a method can only access final methods of the enclosing method
3) A class defined with a method cannot access any of the fields of the enclosing method
4) A class defined within a method can access any fields accessible by the enclosing method


[This message has been edited by maha anna (edited February 26, 2000).]


Rolf Weasel
ranch hand
posted February 27, 2000 03:19 AM             
what does the question mean by methods enclosed within methods? i hope this is a typo or i'm gonna have to read roberts heller again!
i guess that marcus includes local method variables when he says `all fields accessible to the enclosing method'.

Jim Yingst
sheriff
posted February 27, 2000 12:06 PM             
OK, easiest part first - it's "Marcus" not "Morcus".

Next - I double-checked Marcus' tutorial, specifically this part. The question as it appears there is slightly different from what Maha Anna quoted:

quote:

Question 1)

Which of the following statements are true?

  1. A class defined within a method can only access static methods of the enclosing method
  2. A class defined within a method can only access final variables of the enclosing method
  3. A class defined with a method cannot access any of the fields of the enclosing method
  4. A class defined within a method can access any fields accessible by the enclosing method

And so is his answer:

quote:

Answer 1)

2) A class defined within a method can only access final variables of the enclosing method

Such a class can access parameters passed to the enclosing method


Looking closely, he's not saying that the answer is 1); rather, he's saying that for question 1), the answer is 2). Which is probably a good argument for using letters instead of numbers for the answer options.

So, Maha Anna's version of answer 2 is false - a local class can indeed access non-final methods. (And "methods of the enclosing method" didn't really make sense anyway.)

However, I don't like the wording of Marcus' version of answer 2). The only seems vague - it's not clear exactly what is intended to be excluded. A local class can access a lot of different things, not only final variables of the enclosing method. It can not access non-final variables of the enclosing method (i.e. non-final local variables), but it can access non-local variables (i.e. class or instance fields), final and non-final, which are accessible from the enclosing method. I can't tell whether or not Marcus intended to include these in the question, but my best guess is that he forgot about them, and the statement is false.

However answer 4) is correct, as given by Maha Anna. Note that the word "field" means we're not talking about local variables, only class and instance variables. A local class can indeed access any of these which were accessible from the enclosing method. It can't access non-final local variables, but those aren't included in the statement, so the statement is true.

For anyone who isn't convinced that you can access non-final fields from a local class, try this code:

code:
public class Test {
private String field = "private non-final instance field";
private void method() {
class LocalClass {
void method() {
System.out.println("Look, I'm accessing a " + field);
}
}
LocalClass lc = new LocalClass();
lc.method();
field += " again after it's been changed";
lc.method();
}
public static void main(String[] args) {
new Test().method();
}
}

Which prints:

Look, I'm accessing a private non-final instance field
Look, I'm accessing a private non-final instance field again after it's been changed

I think the cause of the confusion is that many books, and Marcus' tutorial, tend to emphasize only the "final local variables" part for local classes, because the "any class or instance fields" part is true for all inner classes (except "static inner" (nested top-level) classes, which can only access static fields). Since the latter is part of the general behavior for inner classes, it's not emphasized as much specifically for local classes and anonymous classes, even though it's still true.

maha anna
bartender
posted February 27, 2000 09:02 PM             
Thank You all so much. I feel I am cared. I won't forget Mr.Marcus spelling from now on. I am sorry about that Mr.Marcus.
Thank you Jim. You are really sincere in answering the questions posted here. Once again thank you all for your valuable time.
regds
maha anna.

Marcus Green
greenhorn
posted February 28, 2000 12:01 AM             
I remember correcting one version of this question, but I guess I had copied it somewhere else and not updated it. Can you recall where the incorrect version is?

Marcus

Jim Yingst
sheriff
posted February 28, 2000 09:11 AM             
Marcus- I'm guessing that question is actually directed at Maha Anna, but just in case, the version I quoted is at http://www.software.u-net.com/J2Tutor/06_03Tut.htm.

maha anna
bartender
posted February 28, 2000 09:59 AM             
Marcus,
I had downloded your tutorial from one of your tutorial links nearly 2-3 weeks before. The downloaded zip file size is 131K. Last modified date of these pages is 9/18/99.But now I could not see the download link of this tutorial in your page. But as Mr.Jim pointed, in this page http://www.software.u-net.com/J2Tutor/06_03Tut.htm the question is there. It still needs some changes I think.
Thank you
maha anna

|