Sunday 16 October 2011

difference between class and instance method initialization .

hello friends i find a topic which looks easy but the concept behind that topic is really beautiful . now how to start with this topic is really interesting for me , because i found some things which really looks different .

so lets start .

JAVA program is built with or created with 2 things . one is class fields , and second is object fields .
so generally we use those methods with the help of object and with the help of class name .
now my question is that how can you access things which are outside from the scope of the method .

just think about what i am saying .

How instance are initialized :-

now take a breath and start , when ever we use object with ( . ) operator at that time how can you access the method . so these type of questions comes in my mind and hurt me the most . but every time i will find a answer for my query .

when ever we create a object at that time we use "new" keyword , and the benefit of the new keyword is that all un-initialized methods are initialized by the new keyword means all methods and all rules ( interfaces ) and all variables are initialized by new keyword . so for initializing is performed in JVM . and this process is performed in constructor . but do you think how the bulky knows about constructor . thats the main thing . because this whole thing is performed in JVM with the machine level instruction and inside machine level and the constructor is known as <init> inside the machine ( JVM )  . so <init> is the instruction behind every instance level initialization and inside <init> this keyword performs its whole process .

How class methods are initialized :- 

now we all know that class methods and instance methods are totally different . but how the class method initialization can be different from instance initialization method . thats the main and pretty interesting thing .
now something will be real different thats why they perform different things in the real world of programming .
now inside every class method or inside every static method we have few instruction like invokestatic , putstatic , getstatic .when ever these types of instruction are found JVM executes <clinit> instruction inside this *cl-init* , "cl" stands for class initialization and whenever this instruction is performed or executed a pretty interesting thing is performed it is known as "the class or interface that declared the resolved field or method is
initialized if it has not been initialized already." from ( jvm specification ) . here resolve methods are those methods which are binded at compile time thats why they are known or called as resolve methods and in java
only static method are binded at compile time so static methods are resolved methods and initialized by <clinit> instruction .

now " we all say that static method can not access instance method directly " so the reason behind this is <clinit> cause .

                                                                                                                      thanks tushar .


No comments: