• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding Init block

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Instance variables are created first.
2) Init blocks are called next (If any)
3) Constructors are called next.


Init block are used for intializes the value for instance variable .

doesn't i and j gets reset to 0 and 0; by the constructor ...as it's use to intialize the value... ??? ... if no value is supplied it sets the value as 0 right.....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Instance variables of type integer are initialized to 0, that's true. But if you set their value in the init block, they won't be reset in the constructor. That would be annoying to have to reinitialize them again in the constructor, wouldn't it ?
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ,I agree but the Constructor would be out of job...if this happens .....! it's loses the important property of it ! !
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main role of the constructor is to allow to instantiate a class, not to reset instance variables.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AS i know instantiate means bringing code or blueprint or class into existence ...right.....so each must have a value ....in case if i don't have Init block these integer specimen i and j would be set to 0.....wat happens wen i specify Init block ...i really doubt on tht ....


losing it's property or giving state(value to instance variable)to the Object....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wat happens wen i specify Init block

In the init block, if you don't initialize i and j, they'll be initialized to 0.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would the constructor initialize variables? A constructor will just do whatever is in the constructor code--in this case, nothing. What if you had the following?Should the constructor wipe out the value of a? No.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Init block are just a way of saying - 'hey put these bunch of statements in each of my constructors'. The idea is to put in all initializing code in one place instead of in each and every one of the constructors.

Init block code is usually in-lined to the beginning of each constructor.

If you de-compile your class, you would see something like this

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Mercs wrote:Init block code is usually in-lined to the beginning of each constructor.
Yes, it appears to be included as part of the constructor. This is what happened when I compiled that class, unchanged, and looked at the bytecode.
javap -c A
Compiled from "A.java"
class A extends java.lang.Object{
int i;

int j;

A();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_3
6: putfield #2; //Field i:I
9: aload_0
10: iconst_4
11: putfield #3; //Field j:I
14: return

public static void main(java.lang.String[]);
Code:
0: new #4; //class A
3: dup
4: invokespecial #5; //Method "<init>":()V
7: astore_1
8: getstatic #6; //Field java/lang/System.out:Ljava/io/PrintStream;
11: new #7; //class java/lang/StringBuilder
14: dup
15: invokespecial #8; //Method java/lang/StringBuilder."<init>":()V
18: aload_1
19: getfield #2; //Field i:I
22: invokevirtual #9; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
25: ldc #10; //String ,
27: invokevirtual #11; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
30: aload_1
31: getfield #3; //Field j:I
34: invokevirtual #9; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
37: invokevirtual #12; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
40: invokevirtual #13; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
43: return

}
You can see the initializer code inside the constructor. Adding i = 5; j = 6; as the constructor body changes the bytecode to this
. . .
A();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_3
6: putfield #2; //Field i:I
9: aload_0
10: iconst_4
11: putfield #3; //Field j:I
14: aload_0
15: iconst_5
16: putfield #2; //Field i:I
19: aload_0
20: bipush 6
22: putfield #3; //Field j:I
25: return
. . .
See what it says in the Java™ Language specification (JLS) about initialisation. I am not sure how useful that section is; if anybody finds a better JLS section, please quote it.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
...doesn't i and j gets reset to 0 and 0; by the constructor ...as it's use to intialize the value... ??? ... if no value is supplied it sets the value as 0 right.....


Have a look on this post
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic