This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes on constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "on constructor" Watch "on constructor" New topic
Author

on constructor

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
hai ranchers
why this code is giving compile time error
can any one explain


Q43.What will be the result of compiling and running the given program?
Select one correct answer.

1. class Q43
2. {
3. int y=1;
4. Q43()
5. {
6. this(y);
7. }
8. Q43(int x)
9. {
10. y=x++ + ++x;
11. }
12. public static void main(String [] args)
13. {
14. Q43 t = new Q43();
15. System.out.println(t.y);
16. }
17. }
a Program compiles correctly and prints 1 when executed.
b Program compiles correctly and prints 4 when executed.
c Compile time error.
d None of above.

by
velan vel
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
Hi

The code doesnot compile because the key word "this" cannot refer to any instance variable but can refer to static variables.

I had the change the code below at //////1 now it works fine and prints 4

Also remember that both this and super keyword cannot have instance variable as arguments but can have static variables.

Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Hi velan,

same type of question has already been asked.
see this:
http://www.coderanch.com/t/252273/java-programmer-SCJP/certification/Superclass-Reference

i think you and Mithran Perry are on the same page.

regards,
Harshil
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: on constructor
 
Similar Threads
this operator
help with go4java again
Calling constructor
supertype constructor
exception