The Artima Developer Community
Sponsored Link

Java Buzz Forum
Non static blocks in java example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Non static blocks in java example Posted: Mar 15, 2016 2:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Non static blocks in java example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
Non Static Blocks in java

  • When ever object created non static blocks will be executed before the execution of constructor
  • Non static blocks are class level block which does not have prototype


  1. package nonstaticblocks;
  2. public class A {
  3.  
  4.    {
  5.         
  6.        System.out.println("non static block executed");
  7.  
  8.     }
  9.  
  10. }


What is the need of Non static blocks in java?

  • To execute any logic whenever object is created irrespective of constructor used in object creation.

Who will execute Non static blocks?

  • Non static blocks are automatically called by JVM for every object creation in java stack area

How many Non static blocks we can create?
  • We can create any number of Non static blocks

Order of execution of non static blocks

  • Order of execution of non static blocks will be order as they are defined.
  1. package nonstaticblocks;
  2. public class A {
  3.  
  4. {

  5.   System.out.println("first block");
  6.  
  7. {
  8.  System.out.println("second block");
  9. }
  10.  
  11. {
  12.  System.out.println("third block");
  13. }
  14. public static void main(String[] args) {
  15.   A obj= new A();
  16. }
  17. }

 Output:

  1. first block
  2. second block
  3. third block


 Order of execution of non static blocks with respect to constructor?



non static blocks in java example program


Read: Non static blocks in java example

Topic: Links for 2016-03-14 [del.icio.us] Previous Topic   Next Topic Topic: The Six Roles of the Interface

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use