• 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

un-named block of code

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while reading i came across this block of code

public class unspec
{
{
System.out.println("Hello");
}
unspec()
{
System.out.println("Hello World");
}
public static void main(String args[])
{
unspec us=new unspec();
}
}


this block of code compiles and the unnamed block is executed first,even before constructor.
What is this block and what is its purpose.

please guide me
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That "block of code" ...



is called an instance initializer. It is executed along with the initialization of your instance variables. In terms of when, it is executed after the super constructor, but before the constructor.

Henry
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With due respect Henry,it still is not very clear.would you or anyone else please explain it.sorry and thanks for the trouble
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jitendra Jha:
With due respect Henry,it still is not very clear.would you or anyone else please explain it.sorry and thanks for the trouble



With due respect, can you tell us what you are not clear with? We can't read your mind, and just repeating ourselves with different words or examples, is not a very efficient.

Henry
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at http://java.sun.com/developer/JDCTechTips/2004/tt0406.html#2

Googling for "instance initializer", it was the first hit.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try searching for "java instance initializer" with Google, and you'll find a number of pages that explain what it is. Maybe these are useful:

The Essence of OOP using Java, Instance Initializers
The Java Tutorial - Initializing Fields (see the bottom of the page)
JavaWorld: Object initialization in Java
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic