• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple question

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}}}

The compiler reports an error at line 2
The class will not compile
The value 10 is one of the elements printed to the standard output
The compiler reports an error at line 9
The class compiles but generates a runtime error

i answered the 4th option,but the correct answer is 3.
i answered 4th, bcos static variables cannot have a this reference.

plz correct me, wht is wrong?
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JayaSiji Gopal:
public class Test {
static int total = 10;
public static void main (String args []) {


At this point, you created an instance of teh class Test. This action invoked the constructor. So when you refer to this in the constructor, you refer to teh class created here. So the 3rd option is indeed correct.
Hope that helps.


new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}}}

The compiler reports an error at line 2
The class will not compile
The value 10 is one of the elements printed to the standard output
The compiler reports an error at line 9
The class compiles but generates a runtime error

i answered the 4th option,but the correct answer is 3.
i answered 4th, bcos static variables cannot have a this reference.

plz correct me, wht is wrong?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic