• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Initializer means what

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract class Abs
{
private int i;
public abstract void first();
{ // What these three lines specifies and when it will be executed
System.out.println("in first");

}
}

Please help me. What ie meant by Instance Initializer here.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you make a subclass of class Abs, and make an object of the subclass then initializer in class Abs gets called as

try this code:




output is:
in first
in second
in SubClass constructor

i hope you are clear now.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

It is the block which will be executed before the constructor when object of a class type is create. but if there is static{} block present in the class type then it will be executed first time for first object and will not be executed for second object.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Take following program for example:


class hai{
static{
System.out.println("Iam in static block");
}
{
System.out.println("Iam in init block");
}
}
class din{
public static void main(String s[])
{
System.out.println("Hello");
hai h=new hai();
hai hello=new hai();
}
}


output:

Hello
Iam in static block
Iam in init block
Iam in init block
 
Kaarthick Ramamoorthy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important to realize that the instance initializer(s) are executed first no matter which constructor is executed. So you can use them for common initialization code.
[ September 04, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly, in the above code they are creating the anonomous inner class. The code present inside the scope is the constuctor of the inner class without any name (ie., anonymous).
What is not clear to me is when this inner class is defined static, how all the instance of the outer class refer to the same instance of the inner class.
That is, conceptually, all the different objects are instantiating the same inner class.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mahantesh

No, those are not anonymous inner classes. They are static blocks and init blocks as explicitly stated. For the exam, one way of spotting anonymous inner class is that following the definition of "new Class()" is an opening curly brace, instead of the usual ";". Then at the end of the closing curly brace, you'll find the semicolon instead "};"

For example:


Or if the anomymous class is argument defined:


In which case the semicolon is after the closing parenthesis "});".
[ September 04, 2006: Message edited by: bing marquez ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic