• 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

very hard question

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------------------------------------------------------
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://forum.java.sun.com/thread.jspa?threadID=151367&messageID=435619
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic