• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

o/p needed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


can any explain the o/p of the pgm?


public class T014
{
static
{
System.out.print(0 + " "); //1
}

{
System.out.print(1 + " "); //2
}

public static void main(String args[ ])
{
T014 t; //3

System.out.print(2 + " ");

t = new T014(); //4

System.out.print(3 + " ");
}
}
they gave o/p is 0 2 1 3
but i m getting 0 1 2 3.


Regards
Suhita
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This block



is not executed until the instance of the class is created.
 
Suhita Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx 4 u r reply.now i understood
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please give me the step by step explanation.because I thought execution will start from main() so
System.out.print(2 + " ");
would execute first.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

first static block will be initialize and 0 will be printed. Then 2 will be printed. After that T014 object will be initialized and 1 will be printed. in the last, 3 will be printed.

Anybody correct me if i am missing something

Regards,
nitin
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply nitin.So,first the static block will execute before main.So we got 0 and then 2 will display .after that for the creation of object() construnctor how is the block 1 is executing.Please help me.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is created, after all superclasses are initialized, the instance variables (and initializers if they have them), and initializion blocks are executed in textual order, then the rest of the constructor is executed.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic