• 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

init blocks example

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



out put:-
Staticblock Parent
Staticblock Parent 2
Exception in thread "main" java.lang.NoSuchMethodError: main


Help me to understand this output....
 
Ranch Hand
Posts: 140
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are running Parent class, you dont have a main method in your Parent class
 
kurt hanni
Ranch Hand
Posts: 140
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- remove public in Parent class
- make Child class public
- rename file to Child.java
- compile Child.java
- run Child
 
Vidya sumanasekara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kurt hanni wrote:You are running Parent class, you dont have a main method in your Parent class




yes .... Thanks..

Then output:-

Staticblock Parent
Staticblock Parent 2
Staticblock Child
Initblock Parent
Constructor Parent
Initblock Child
Initblock Child 2
Constructor Child A
Constructor Child

Can you explain me....
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vidya sumanasekara wrote:

kurt hanni wrote:You are running Parent class, you dont have a main method in your Parent class




yes .... Thanks..

Then output:-

Staticblock Parent
Staticblock Parent 2
Staticblock Child
Initblock Parent
Constructor Parent
Initblock Child
Initblock Child 2
Constructor Child A
Constructor Child

Can you explain me....

Hello Vidya, study the following VERY carefully, I hope it helps:

1. INIT BLOCKS EXECUTE IN THE ORDER THEY APPEAR

2. STATIC INIT BLOCKS RUN "ONLY ONCE" WHEN THE CLASS IS "FIRST" LOADED BY THE JVM

3. INSTANCE INIT BLOCKS RUN "EACH TIME" A CLASS IS INSTANTIATED

4. INSTANCE INIT BLOCKS RUN "IMMEDIATELY AFTER" THE CONSTRUCTORS CALL TO super()

With the above explanation you can see that the other of execution is:

1. Static init block
2. Instance init block
3. Constructors
[/quote

 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic