aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About static method!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About static method!!!" Watch "About static method!!!" New topic
Author

About static method!!!

junming zhang
Greenhorn

Joined: Nov 03, 2001
Posts: 8
Q.
Consider the following classes:
public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}
}
What is the result of compiling and running this class?
A.The string Test is printed to the standard out.
B.A runtime exception is raised stating that an object has not been created.
C.Nothing is printed to the standard output.
D.An exception is raised stating that the method test cannot be found.
E.An exception is raised stating that the variable this can only be used within an instance.
F.The class fails to compile stating that the variable this is undefined.
************
given answer is F.but I cann't know it clearly.if you think the answer is right,please explain it to me in detail.thanks for your reply!

Dream first and Do later
fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi, there
i think the answer is not right. because "this" is always refered to a certain object. so it meas it can not be refered within a static context.
any commment?

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi,
the given answer is clearly wrong.
The correct answer is E since you can't reference this within a static context.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
<pre>public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}

public static void main(String args []) {
test();
}

}</pre>
What is the result of compiling and running this class?

On compile I get:

C:\Java\Test.java:4: non-static variable this cannot be referenced from a static context
this.print();
^
1 error

[This message has been edited by Marilyn deQueiroz (edited November 18, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About static method!!!