This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what compiler do & what JVM do ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what compiler do & what JVM do ..." Watch "what compiler do & what JVM do ..." New topic
Author

what compiler do & what JVM do ...

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213


Hi guys ,
This may be a very basic question for you all , but please help me to understand what is happening at what time . I mean what happens after compilation & what happens at the run time . What compiler check & what don't check and leave for JVM ...

I hope the question is very clear .

Thanks in advance .
Pooja


:d
Vivek Mongolu
Ranch Hand

Joined: Apr 17, 2003
Posts: 42
code:


class Parent {
int i = 8 ;
static int j = 7 ;
static {
int k;
}
Parent() {
k = 6;
i = 23;
}
public static void main(String[] s) {
Parent p = new Parent();
System.out.println(j);
p.j = 2 ;
System.out.println(j);
System.out.println(p.i);
p.i = 2 ;
System.out.println(p.i);
System.out.println(p.k);
p.k = 2;
System.out.println(p.k);
}
}

This wont compile. reason being k is declared in the static block. so any code outside that block wont be able to access k. hence it gives compile time errors at line 8, 19 20, 21.
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
OK vivek you are right , now I am changing my code , please solve my doubt and please also tell me that why we can't access a variable defined in static block ( at the same time , we can access static variable ).
thanks in advance , pooja

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
As a matter of fact I do not understand your question, but I can tell you what happens when you compile it and execute it.

At compile time javac checks grammar, sintax and type assignmets, if that's right it will generate the class file.

Al run time, when you create the new instance of T the classloader loads the class and initialize its static members also execute the static initializer block. By the way, you cannot access a variable declared in the initializer block because it just exists within that context, that's to say, after static initializer is done the variable is destroyed. It's like any other code block.

After the class loader has loaded the class and initialized the static members of the class the JVM will initialize the instance members of the class, starting from the parent class (which in your snippet case is Object), also the instance initializer blocks will be executed. Then the constructors will be executed starting from the parent class (in this case once again Object).

Then from the main method you ask the system out object to print the static member j, whose value will be the same for all instances of T, because it belongs to the class, not the instance.

You could have also modified its value this way
T.j = 2;
t.j = 2

or simply
j=2;

You can do that because instances of classes can access its static members, the contrary is not true, that's to say an instance member canno be acccessed from a static context. Why not? Because instance members belong to a an object (instantiated class) and cannot be accessed until an object exist.

When you change the value of j you change it in the class, so it change for all instances of T. It is not the same with i, because it is an instance member, and when you change its value it just changes for the instance t.

How to know if this is what you wanted to know?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
whatever pooja say ... but I liked it very much .
But Edwin there is somethig wrong in this
Originally posted by Edwin Dalorzo:
Al run time, when you create the new instance of T the classloader loads the class and initialize its static members also execute the static initializer block.


class loader load the class & initialize static variable & execute static block ... even if you don't create instance of class .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Classes are loaded by the JVM either when the first object of that class is created or the first time a static member of that class is accessed -- even if no object is created. Alternatively, a class can be loaded manually using Class.forName(String).

In this case, T.class is actually loaded when main is invoked, because main is a static method of T.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
marc , i got your this point .
can you explain my first q in more detail .
thanks a lot , pooja
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think Edwin explained it pretty well. Is there a specific part of this that you're questioning?
 
 
subject: what compiler do & what JVM do ...