GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes init blocks example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "init blocks example" Watch "init blocks example" New topic
Author

init blocks example

Vidya sumanasekara
Greenhorn

Joined: Feb 03, 2011
Posts: 10



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


Help me to understand this output....
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

You are running Parent class, you dont have a main method in your Parent class


SCJA, SCJP, SCJD, SCMAD, SCWCD, SCBCD, SCDJWS, SCEA, SOA, Spring Certified, OCP MySQL 5 Developer, PMP
kurt hanni
Ranch Hand

Joined: Aug 11, 2007
Posts: 140

- remove public in Parent class
- make Child class public
- rename file to Child.java
- compile Child.java
- run Child
Vidya sumanasekara
Greenhorn

Joined: Feb 03, 2011
Posts: 10
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....
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init blocks example