• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what compiler do & what JVM do ...

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Vivek Mongolu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc , i got your this point .
can you explain my first q in more detail .
thanks a lot , pooja
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Edwin explained it pretty well. Is there a specific part of this that you're questioning?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic