File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Core Concept needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core Concept needed" Watch "Core Concept needed" New topic
Author

Core Concept needed

vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
Posts: 21
Hi,
The following code gives the compilation error

public class AQuestion
{
private int i = j;
private int j = 10;



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



This code executes corrrectly and gives the output 0

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

Why is it so bcos in both the cases "j" is declared after i.

thanks in advance
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
The first code : you have to declare file j before field i;
The second code : var j in method not the same field j in class. You can break point in System.out.println((new AQuestion()).i).
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Hi Vijaya,


Why is it so bcos in both the cases "j" is declared after i.


The order of declaration is not making any difference in the code you mentioned. Whenever a class in instantiated, all the instance variables are given thier default values. However, before the initialization is done, you can not assign any existing variable or can not refer any existing reference.

in the second code you posted,
is not assigning variable 'j' to 'i' but it's assigning value '0' returned by method 'giveMeJ()' to 'i'. So it's legal.

hth.
vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
Posts: 21
Harshil Mehta ,
as you said in the 2nd code the method returns "j" but when we see the flow of control in the first line it calls the method and at that time "j" is not even declared

pls. explain i'am new
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Vijaya,


as you said in the 2nd code the method returns "j" but when we see the flow of control in the first line it calls the method and at that time "j" is not even declared.


"i" and "j" both are declared and are assigned their default values as soon as object of the class is loaded in the heap. but this initialization does not allow us to refer them in assignment or any arithmatic operation.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Originally posted by Harshil Mehta:
The order of declaration is not making any difference in the code you mentioned. Whenever a class in instantiated, all the instance variables are given thier default values. However, before the initialization is done, you can not assign any existing variable or can not refer any existing reference.


Maybe I misunderstood you, but when we switch these two lines:

private int i = j;
private int j = 10;

to:

private int j = 10;
private int i = j;

the following code compiles and the output is 10.

public class AQuestion
{
private int j = 10;
private int i = j;;

public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Concept needed
 
Similar Threads
one Construtor's problem?
forward refrerences
another perplexing program
please explain why the compiler is not complaining and why the output is 0
Variable initialization