File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes very hard question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "very hard question" Watch "very hard question" New topic
Author

very hard question

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
hai ranchers
here how is ans is 1, 20
can any one explain


class TestClass
{
int i = getInt();
int k = 20;
public int getInt() { return k+1; }
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i+" "+t.k);
}
}

------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
To answer this question you need to understand the Flow of a Java Program.

When a class is loaded,
static variables are initialized and static blocks are executed.
If an object is created,First of all super() is executed on the constructor, then instance variables are initialized and non-static initializer blocks are executed, then the constructor body.

Now consider your code,

class TestClass
{
int i = getInt();
int k = 20;
public int getInt() { return k+1; }
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i+" "+t.k);
}
}



When "new TestClass();" is executed then first super() is called,here the super will call Object class constructor & execute & then instance variables are initialized, so first line of code now is int i = getInt();
this will initialise i=1; as because till k=20 is not been initialised & getInt(); will return 0+1=1,
Thats why the result is 1,20.

------
If you write the code as
class TestClass
{
int k = 20;
int i = getInt();
public int getInt() { return k+1; }
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i+" "+t.k);
}
}

the result will be as 21,20


Hope this will help you.
apoorba mohapatra
Greenhorn

Joined: Mar 28, 2005
Posts: 7
output is 21,20 for
------------------

class TestClass
{
int k = 20;
int i = getInt();

public int getInt() { return k+1; }
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i+" "+t.k);
}
}
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

http://forum.java.sun.com/thread.jspa?threadID=151367&messageID=435619


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thankyou, Apoorba - I just realized I had cut and pasted the wrong piece of code. I deleted my erronous post juat as you posted.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
Hi, apoorba mohapatra

I did not understand anything of your comment. (What & why you have written this. )

Is there anything wrong in my explanation ? Please rectify me if i am wrong.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Purujit Saha:
Hi, apoorba mohapatra

I did not understand anything of your comment. (What & why you have written this. )

Is there anything wrong in my explanation ? Please rectify me if i am wrong.


The confusion is my fault, the post is a response to my (now deleted) erronous reponse. See just above your last post.
-Barry
 
Don't get me started about those stupid light bulbs.
 
subject: very hard question
 
Similar Threads
No Printer Found
Simple Question!!!!
initilization of method
Forward referenced variable in initializer block
getInt()