File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About static method!!!

 
junming zhang
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
fengqiao cao
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic