• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About static method!!!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic