File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes order of loading?????? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "order of loading??????" Watch "order of loading??????" New topic
Author

order of loading??????

Michael Lin
Ranch Hand

Joined: Dec 04, 2000
Posts: 31

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);
}
}
Answers
1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No Compilation error - The output is 0;
4.No Compilation error - The output is 10;
//answer is 3.
//but if the j is static, answer is 4.
can anyone explain why???
thanks!
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
The order of loading :
1. static vars / blocks in the order specified in the class
2. Instance vars block in the order specified in the class
3. Constructor
Hence the answer is 4 when the var is changed to static
asheesh talwar
Ranch Hand

Joined: Dec 10, 2000
Posts: 31
Bharatesh I am not getting why for the first
time the o/p is 0 & not 10 & when we change it to static
why are we getting 10 ?
Can U please explain the logic behind this.
Regards
Asheesh
Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
Hi Asheesh :
Please read these two posts by Maha Anna.They are preety good and will help you understanding the concepts.
http://www.javaranch.com/maha/Discussions/Declaration_And_Access_Control/final_variable_-_JavaRanch_Big_Moose_Saloon.htm

and
http://www.javaranch.com/maha/Discussions/Declaration_And_Access_Control/Another_qstn_for_you_all____Just_to_enforce_the_point___-_JavaRanch_Big_Moose_Saloon.htm

Thanks,
Rajiv
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: order of loading??????
 
Similar Threads
Angelfire.com....quiz 2
Abilash's mock exam, #11
Access Modifiers
Angelfire.com....quiz 2
Doubt