• 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

simple question

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

 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic