• 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

a simple quetion,help

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when will a block without static(like this:
{//code})be execute???
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code within {} is known as an instance initializer and will be executed when an instance of that class is initialized. You can look in the JLS, §8.6 Instance Initializers for details.
Corey
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will execute each time you construct by new Xxx() and execute before the constructor.
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you explain this ? thank you very much!

/*
456
AAA
123
CCC
BBB
*/
/*

/*456
123
AAA
CCC
BBB
*/
/*

/*123
456
AAA
CCC
BBB
*/

/*456
AAA
123
CCC
BBB*/

[ April 17, 2002: Message edited by: Cindy Glass ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will attempt to explain the first one ...
The rest you can figure out ...

I changed the class names.
The output is:
456
AAA
123
CCC
BBB
Here is what happens:
1. DerInvTst1 constructor with no args is called.
2. This constructor calls DerInvTst1 constructor that takes an int. arg.
3. Since there is no "this()" call on the first line, the super class's default constructor is called and super class is initialized.
4. This results in printing 456.
5. Next AAA is printed from BasInvTst1 constructor.
6. Now the derived class initialization occurs
and "123" is printed.
7. The control is then transfered to the constructor, which prints "CCC"
8. In the end the control is transfered back to DerInvTst1's no-arg constructor and "BBB" is printed.
Hope this helps,
Brian
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seems that the initialiaze block is not executed before the constructor??? (because the 123 is not the first line of output)
can you tell me the exactly time when the initialize block will be executed???
[ April 17, 2002: Message edited by: andy lau ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic