I want to know output of this program and the flow alsopublic 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); } }

Questions by sudha_e   answers by sudha_e

Showing Answers 1 - 22 of 22 Answers

Latha

  • May 30th, 2006
 

Flow will be like this..
It will create a Aquestion object. In that process,  first is variable initialization.
By default all class level int variables will be initialized to 0.
So i and j will be 0
then is the step to execute i = giveMeJ(). At this point of time giveMeJ() will return 0.
so i is still 0, then to execute j = 10; so j becomes 10.

Next is to execute (new Aquestion()).i   i is still 0. so prints 0..
if the program is like this

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);
}
}


the output will be 10.

  Was this answer useful?  Yes

sowjanya

  • May 30th, 2006
 

no

  Was this answer useful?  Yes

Devang

  • May 30th, 2006
 

The answer will be 10

  Was this answer useful?  Yes

/*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);
}
}

  Was this answer useful?  Yes

Bret

  • Jun 6th, 2006
 

As far as I can see this code would result in a compile-time error because you are trying to access a private member on a new instance of an object in the println statement.

  Was this answer useful?  Yes

sasmita

  • Jun 9th, 2006
 

0

  Was this answer useful?  Yes

Ashok Agnihotri

  • Nov 19th, 2007
 

I think this code will not compile becoz you are trying to call a method() without object. if i m wrong then please corrects me. thanks for this wonderful question.

  Was this answer useful?  Yes

AjayMukiri

  • Apr 30th, 2008
 

Hi Latha

as per my knowledge methods will be called with dot(.) operator only, So can we assign a variable with method value. If we assign also you need to call the method with a class object using dot operator. Please corret me if I am wrong.

  Was this answer useful?  Yes

compile sucessfully and print 0

logic -->
step 1 -- Load the class (here all primitive variables are having default values)
step 2 -- execute main() menthod
now to create object.......
step 3 -- execute constructor (default here)
here values are also assigned
step 4 -- print the values

  Was this answer useful?  Yes

talktoatish

  • Aug 16th, 2009
 

public class Foo {

private
static int j = 10;

private
static int i = giveMeJ();

private static int giveMeJ() {

System.out.println("in the method" + i + j);

return
j;

}
public static void main(String args[]) {System.out.println(i);

}
}

After changing the i and j declaration,
The result will be
in the menthod 0 10
10




  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