• 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

help me plz

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help me in understanding the difference between the initialization of static block n non-static block. which block gets initialized first n how it gets initialized.
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see reehan ,
for interpreter of every lang. there is some enty point
in case of 'C' lang it is main() function
i.e while running the c prg the interpreter looks for main
function. same is in case of java but static variables &
block has higher prority so all the static blocks r first
executed & the main method get's chance
hey do u know u can execute ur prg with out main method.
try this code

public class Mission_impossible
{

static
{
System.out.println("Who said main is required....?");
System.exit(0);
}
}
so the exection steps r as follows
1 : execute sataic block if any
2: initilize satatic variables if any
and lastly execute main method
hope this help u
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static block gets executed when class is first loaded. For eg.:
class Test {
static String var;
static {
var = "Test";
}
}
then when you first access class Test like that Test.var, static blok gets executed thus initializing var to "Test". No class instance is created. But this happens only once. If you call Test.var once more static block does not get executed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic