I want to know the difference between these two programs mean the flow/*public class Aquestion { private int i = giveMeJ(); private int j = 10; private int giveMeJ() { return j; } public static void main(String args[]) { System.out.println((new Aquestion()).i); } } */public class Aquestion{private int j = 10;private int i = giveMeJ();private int giveMeJ(){ return j; }public static void main(String args[]){System.out.println((new Aquestion()).i);}}

Questions by sudha_e   answers by sudha_e

Showing Answers 1 - 16 of 16 Answers

Sachin Goswami

  • Jun 1st, 2006
 

In the first class when we try to access j value but the initilization of j happen after method call so value is given zero .

In the second class j value is initialiae then it call then it give you result 10

  Was this answer useful?  Yes

sbarik

  • Jun 6th, 2006
 

The 1 st programm is Equivalent ti this:--->>>

public class Aquestion

{

public Aquestion()

{

i = giveMeJ();

j = 10;

}

private int giveMeJ()

{

return j;

}

public static void main(String args[])

{

System.out.println((new Aquestion()).i);

}

private int i;

private int j;

}

Wheras in 2nd on the flow will be like this:-->

public class Aquestion

{

public Aquestion()

{

j = 10;

i = giveMeJ();

}

private int giveMeJ()

{

return j;

}

public static void main(String args[])

{

System.out.println((new Aquestion()).i);

}

private int i;

private int j;

}

  Was this answer useful?  Yes

Sanjeev Chaubey

  • Jun 12th, 2006
 

Hi Friend

In Java All Calss variable Defaule Values is 0;

so the first program define i values in 0 b'coz j in not difine and other programe j is define first so the method call and int. the value of i is j;

  Was this answer useful?  Yes

mahesh

  • Jun 13th, 2006
 

/*public class Aquestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{ return j; }
public static void main(String args[])
{
System.out.println((new Aquestion()).i);
}
} */

hi

In  the first program ---during the class loads all the primary data types are assigned to their default values ...so here while class loading ,before the object is created the values of i and j are 0. after the object is created and invoking the i variable it internally calls the giveMeJ() which intern returns the j value that is 0;

public class Aquestion
{
private int j = 10;
private int i = giveMeJ();

private int giveMeJ()
{ return j; }
public static void main(String args[])
{
System.out.println((new Aquestion()).i);
}
}

In the second program--once j value is assigned as 10 after the object creation so when the i value is called it internally calls the method and returns

j as 10 on consle

  Was this answer useful?  Yes

Brju

  • Jan 15th, 2007
 

first program prints 0

second prints 10

  Was this answer useful?  Yes

Guest

  • Jan 22nd, 2007
 

and also remember only class objects if not initialized are initialized to default values... for eg, class variable int i will be assigned with 0. 

  Was this answer useful?  Yes

rahul singh

  • Jul 25th, 2007
 

?In first program,
?????
when j was initialize to i via method giveMeJ(),the value of j is 0.
????? therefor i is initialized by 0.
in second program,
?????
when j was initialized to i via giveMeJ(),value of j is 10.
????? in line no 1,j has initialize by 10 .
????? after that we assign the i variable so the value 10 is gone to i.

??? but in 1st program,
intializing to i is being before initializing to j.so the 0 value has gone to i.
I hope that you can undestand my answer.if you have any problem,mail me at rahulsingh.ktr @ gmail . com.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions