Static initialization block yuav raug hu ua soon as class A loads. Instance initialization block yuav raug hu tsuas yog thaum tus tsim qauv ntawm chav kawm raug hu los tsim ib qho khoom ntawm chav kawm A.
Thaum twg koj yuav siv qhov thaiv qhov pib zoo li qub?
A Static Initialization Block hauv Java yog qhov thaiv uas runs ua ntej lub ntsiab() txoj hauv Java. Java tsis quav ntsej yog tias qhov thaiv no tau sau tom qab lub ntsiab() txoj kev lossis ua ntej lub ntsiab() txoj kev, nws yuav raug tua ua ntej txoj kev tseem ceeb() tsis hais.
Thaum qhov thaiv zoo li qub hauv Java raug tua?
Static blocks execute thaum cov chav kawm yog loaded rau hauv lub cim xeeb whereas instance blocks execute tsuas yog thaum piv txwv ntawm chav kawm yog tsim. 5. 'qhov no' lo lus tseem ceeb tsis tuaj yeem siv hauv qhov thaiv zoo li qub thaum lo lus tseem ceeb no tuaj yeem siv rau hauv qhov piv txwv.
Dab tsi yog cov blocks zoo li qub thiab cov pib zoo li qub hauv Java?
Nyob hauv java, peb tuaj yeem siv lo lus tseem ceeb zoo li qub nrog lub thaiv ntawm cov lej uas hu ua static block. Ib qho thaiv zoo li qub tuaj yeemmuaj ntau cov lus qhia uas ib txwm khiav thaum ib chav kawm raug thauj mus rau hauv lub cim xeeb Nws tseem hu ua java static initializer block vim peb tuaj yeem pib qhov hloov pauv zoo li qub hauv qhov zoo li qub thaiv ntawm lub sijhawm.
Puas yog qhov thaiv zoo li qub raug tua ua ntej tus tsim?
Nco: Static blocks kuj tuaj yeem raug tua ua ntej tus tsim.