Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this from main method

 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Example {
int x=10;
// Lots of code including a proper main method
}
1. this.x is accessible from the main method with any access
specifier for x.
2. this.x is accessible from main method if the declaration of x is
private
3. this.x is accessible from main method if the declaration of x is
public
4. This.x is accessible from main method without any changes to the
existing code
what's the correct answer? is it 4??

-Arun
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe that any of the choices are correct as none of the following code snippets compile:


Most importantly, you should remember that the main method is static and therefore can not directly access any instance members of it's enclosing class. The following, however, is legal and does compile.
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodney , I have modified to replace the instance myEx to This

But, note the "This.x"
word in the last answer !!. "This" is entirely different from "this".
So, you have to assume that, there is a class instance "This" and you
are accessing the x bound to that instance. So, the answer should be 4th
one. But, in the question it is never specified that an instance is
created and assigned to the variable name "This". You have to assume.
Sad thing is that even after so much careful design of the exam, quite a lot has been left for our assumption. This is what Kai mentions in his Study notes, is he Right???
-Arun

[ Added UBB [ CODE ] tags to add whitespace, improve code readability ]
[ May 11, 2002: Message edited by: Jessica Sant ]
 
imran anwar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
can you provide some link to Kai's notes, if its available online ?
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic