• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Instance initialization block error

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have the following code



OUTPUT
------
case 1:

if i exceute WITHOUT line 2 and line 3 it prints like this

Instance block executed
Initializations made are x:0 y:3

Here my question is , eventough i was intializing static variable x to 4
in instance block it's not getting. Why,please help me out.

CASE 2::
if execute WITH line 2 and 3 but without line#1

InitError ob=new InitError();
System.out.println("Initializations made are x:"+x+" y:"+ob.y);

for this OUTPUT

Instance block executed
Initializations made are x:4 y:3

why is this behaviour?
Thanks.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

So this block



is an instance block.

When you run this program, the java interpreter enters the main method, whose first statement is line #1. The string is read from left to right.

The key is this question: When do instance blocks get called?
[ August 03, 2008: Message edited by: Nikos Pugunias ]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its simple. there are certain sequece followed when loading classes.

> all the static variables & methods (in the order they occur)
> all the init blocks ( in the oder they occur)
> the constructor that was invoked using new

case 1:
when you are accesing the var x .. it has only been declared (so its default value is 0) init block hasn't been executed yet
but when accessing the variable y you are creating a new instance of class & so the init block & constructors are executed & so y becomes 3 ( so x becomes 4 ... check it)

Case 2:
in this case you are creating the instance of class 1st before accessing the vartiables so they are initialised to x = 4 & y = 3
 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nikos and Milan.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic