I was reading about static init block and they are loaded when the class is loaded if there are multiple classes in the source file and there are multiple static init blocks in each of them then what will be the sequence
will it be in the order they are declared in the file?
does inheritance have any effect on this(i doubt it though) I mean if there are 3 classes A, B and C declared in the same order B inherits A and C has the main function does it make a difference?
Just a small confusion Thank you in advance Thomas
Originally posted by T George: So I guess that means all the superclasses of the class that is first loaded are loaded and nothing else even if they are in the same file
You need to distinguish between source files and byte code (.class) files. You can put as many classes as you want in a source file (as long as only one top level class is declared public), but when you compile it you will get a separate .class file for each class. So when you load a class, there are no other classes in that file to load and hence no order to maintain.
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Joined: Jun 26, 2008
Originally posted by Joanne Neal:
So when you load a class, there are no other classes in that file to load and hence no order to maintain.
Thanks for the help Joanne I did think about this but i thought there might be some info about the source file(like names of other classes or something) stored in the class file(byte code) which I don't know about(fairly new to java ) Maybe I should check what goes into a class file in JLS(i think) however it is clear that it does not matter whats in the source file regarding which class is loaded
@Bill Thanks for the link JLS seems a lil out of my league but I will try to use it once I am comfortable with the language
Thank you again for the help much appreciated Thomas
Write yourself a tiny class, compile it to bytecode with javac then analyse it with the javap tool.
javac MyTinyClass.java javap -c MyTinyClass
You get a printout of what the individual bits of bytecode mean; they look quite like assembler. You will find that other classes quoted in your original .java file reappear in that printout, so their name is retained in the bytecode. As far as I know, the class is loaded the first time it is mentioned in the code.
You would load the class with the main method first, then java.lang.String because the main method has String in its parameters. Then whichever classes are mentioned in main.