Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very hard question

 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://forum.java.sun.com/thread.jspa?threadID=151367&messageID=435619
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou, Apoorba - I just realized I had cut and pasted the wrong piece of code. I deleted my erronous post juat as you posted.
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic