File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this from main method" Watch "this from main method" New topic

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
3. this.x is accessible from main method if the declaration of x is
4. This.x is accessible from main method without any changes to the
existing code
what's the correct answer? is it 4??

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???

[ Added UBB [ CODE ] tags to add whitespace, improve code readability ]
[ May 11, 2002: Message edited by: Jessica Sant ]
imran anwar

Joined: May 06, 2002
Posts: 12
can you provide some link to Kai's notes, if its available online ?
I agree. Here's the link:
subject: this from main method