File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question regarding Init block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding Init block " Watch "Question regarding Init block " New topic
Author

Question regarding Init block

Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

The main role of the constructor is to allow to instantiate a class, not to reset instance variables.
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Feb 08, 2009
Posts: 606

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



Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39387
    
  28
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

Joined: Nov 04, 2009
Posts: 2066

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


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
thank you!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding Init block