This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this from main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this from main method" Watch "this from main method" New topic
Author

this from main method

Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
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

Joined: Dec 04, 2001
Posts: 80
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.


Hope This Helps
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
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

Joined: May 06, 2002
Posts: 12
Arun,
can you provide some link to Kai's notes, if its available online ?
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this from main method
 
Similar Threads
Final keyword???
One more
kai's notes
confusing valid answer to a jq+ question
this reference in static class