Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance blocks.  RSS feed

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will also excute :

public class PreInit{
{
x = 10;
//System.out.println(x);
}
int x;
{System.out.println(x);}
public static void main(String[] args){
PreInit pi = new PreInit();
System.out.println(pi.x);
}
}

what I understood in other words is:
The operations include assigning to the variable " the variable is in the left side of the = sign are permitted before declaraion"
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags for posting the code. It will be easier to follow.

Useful article related to Forward Referencing.
 
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey rohit.... let me try to clear your doubt about initialization.

this i read from Sun study guide..

when you create any instance of the class then following steps are done..

e.g A a = new A(123,"xyz");

1) Bind constructor parameters

2) call this() recursively until super() is found

3) call recursively super in inheritance tree from child class to parent class.

4) after super call finished do initialization activity (as i have tested first explicit initialization of values will be done e.g. class A{int i=10; // this will run} and then after initialization blocks will run so that your explicit initialization values are changed by values in initialization blocks).

5)run remaining constructor code.....

I hope this will clear your doubt about initialization...

 
Ranch Hand
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit,

I haven't read all the replies of this thread so i am sure whether this has been mentioned or not but i think your doubt and code are not at all converging the same things.
Below are things for you.

1. In Java there is nothing like instance blocks. There are Initialization blocks which are used for instance variable initialization (This is what you are looking for).
2. Initialization blocks are executed before any constructor execution. Please look below code for the veracity of my statement.



Ouput:
Hello
4

Now, as constructors are executed after initialization block, after uncommenting your constructor "index" value is overridden and hence output is '8'.
Please feel free if any doubts.

Thanks.
 
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey hello Imad Aydarooos
The problem with

is that x is an instance variable so Initialization block wont know variable of which instance to print.
Because at this stage i think only the superclass constructors have run and object is created but no reference is assigned to the object so it wont know that we are accessing this objects instance variable unlike when using setter a method to assign value to the instance variable .
Now so if you try this it works

OR you can make it static it works .


Thank you for bringing this up .
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey hello Imad Aydarooos
The problem with

is that x is an instance variable so Initialization block wont know variable of which instance to print.
Because at this stage i think only the superclass constructors have run and object is created but no reference is assigned to the object so it wont know that we are accessing this objects instance variable unlike when using setter a method to assign value to the instance variable .
Now so if you try this it works

OR you can make it static it works .


Thank you for bringing this up .
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:This will also execute :



For the above reply
Apology for posting it two times .
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pratik D mehta wrote:Hey hello Imad Aydarooos
The problem with

is that x is an instance variable so Initialization block wont know variable of which instance to print.
Because at this stage i think only the superclass constructors have run and object is created but no reference is assigned to the object so it wont know that we are accessing this objects instance variable unlike when using setter a method to assign value to the instance variable .
Now so if you try this it worksThank you for bringing this up .



But Pratik, its already working, I mean the code in my reply is already working and its output is (10 and another 10), i posted the code to demonstrate how to do a forward initilization for instance variable and again : there is no problem in the code... Its working
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:
2: This will produce (PreInit.java:4: illegal forward reference
System.out.println(x);

^
public class PreInit{
{
x = 10;
System.out.println(x);
}
int x;
public static void main(String[] args){
PreInit pi = new PreInit();
System.out.println(pi.x);
}
}

this shows that forward initialization is permitted, but forward accessing is forbidden
meaning: using the variable at the lift of (=) is permitted, and vise versa.


Ok i was talking about this code where there is Illegal forward reference compilation error .
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow!
These kind of discussions indeed give learners like me the required zeal to study deep and understand the topic from the root!
Hats off to all of you!

But, can someone please give the final outcome of this discussion? I got confused in the middle trying to figure what the right execution order is.

What is the right order of execution?? is it the one mentioned by Stephan?
Please help!
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rikesh Desai wrote:Wow!
But, can someone please give the final outcome of this discussion? I got confused in the middle trying to figure what the right execution order is.
What is the right order of execution?? is it the one mentioned by Stephan?
Please help!



I think if you run this code you will find a prif of what have been discussed here:

The output is:
in static init block.
in Super Constructor
in Sub instance init block. x value is: 10
in Sub constructor init block. x value is: 20
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!