Non static blocks in java example

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


Remove all elements LinkedHashSet example

1.Basic java example program to remove all elements in linkedhashset
  • clear()   This method used to remove all elements from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  linkedhashset.clear();
  34.  System.out.println("LinkedHashSet after removal  all elements"); 
  35.  
  36. }
  37.  
  38. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. LinkedHashSet after removal :
  5. Java Interview Questions
  6. Java interview program
  7. Concept and example program
  8. Concept and interview questions
  9. LinkedHashSet after removal all elements  :[]

Differences between Default constructor and no argument constructor in java

Differences between default constructor and no argument constructor


Default Constructor in java:

  • When we write a class without any constructor then at compilation time java compiler creates a default constructor in our class.
  • The accessibility modifier of the default constructor is same as accessibility modifier of class.
  • The allowed accessibility modifier are public and default.
  • Default constructor added by java compiler this constructor does not have anything except super(); call.





  1. package constructor;
  2. public class A {
  3.   
  4.  
  5. }


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.         super();
  7.  
  8.     }
  9.  
  10. }



  • If our class have any constructor then java compiler does not create default constructor

No-argument Constructor in java:

  • As a developer we can create our own constructor with no arguments is known as no-argument constructor.
  • It can have all four accessibility modifiers as it is defined by developer.
  • So allowed accessibility modifiers are public, private, protected and default
  • It can have logic including super call.


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.   super();
  7.   System.out.println("no -argument constructor");

  8.     }
  9.  
  10. }

  • The common point between default and no-argument constructor 
  • Both does not have any arguments.
  • And one more point we need to remember that in no-argument constructor also by default first statement will be super() call which is added by java compiler if it does not have.

Remove specified element from Java LinkedHashSet example

1.Basic java example program to remove particular element in linkedhashset
  • boolean remove(Object o)    This method used to remove specified element from Linkedhashset.

  1. package com.removeelementLinkedhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.       
  19. System.out.println("LinkedHashSet before removal : " + linkedhashset);
  20.  
  21.   boolean blnRemoved = linkedhashset.remove("Java Quiz");
  22.   System.out.println("Was Java Quiz removed from LinkedHashSet ? " + blnRemoved);
  23.   

  24.  
  25. System.out.println("LinkedHashSet after removal : ");
  26.  
  27. Iterator it=linkedhashset.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. }
  35.  
  36. }
     



Output:

  1. LinkedHashSet before removal : [Java Interview Questions, Java interview program, Concept
  2. and example program, Concept and interview questions, Java Quiz]
  3. Was Java Quiz removed from LinkedHashSet ? true
  4. Java Interview Questions
  5. Java interview program
  6. Concept and example program
  7. Concept and interview questions


Check if a particular element exists in Java LinkedHashSet Example

1.Basic java example program to check particular element is exists in linkedhashset
  • boolean contains(Object o)   This method Returns true if this set contains the specified element

  1. package com.checkelementhashset;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.Iterator;
  5.  
  6. public class LinkedHashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. LinkedHashSet<String> linkedhashset = new LinkedHashSet<>();
  11.        
  12.         linkedhashset.add("Java Interview Questions");
  13.         linkedhashset.add("Java interview program");
  14.         linkedhashset.add("Concept and example program");
  15.         linkedhashset.add("Concept and interview questions");
  16.         linkedhashset.add("Java Quiz");
  17.     
  18.  /*
  19.  To check whether a particular value exists in LinkedHashSet we need to use
  20.   boolean contains(Object value) method of HashSet class.
  21.  this method returns true if the LinkedHashSet contains the value, otherwise returns false.
  22.  */
  23.        
  24.         boolean isExists = linkedhashset.contains("Java Quiz");
  25.         System.out.println("Java Quiz exists in LinkedHashSet? : " + isExists);    
  26.  

  27.  
  28. Iterator it=linkedhashset.iterator();
  29.              
  30. while(it.hasNext()){
  31. System.out.println(it.next());
  32.                      
  33. }   
  34.  
  35. }
  36.  
  37. }
     



Output:

  1. Java Quiz exists in LinkedHashSet ? : true
  2. Java Interview Questions
  3. Java interview program
  4. Concept and example program
  5. Concept and interview questions
  6. Java Quiz

Return statement in finally block in java

Can we write return statement in finally block

  • Finally block will executes always excepts system.exit().
  • So if we are returning some value in finally means it will be returned always
  • Finally will executed so method always returns finally return value and no need of keeping return value at end of the method.
  • And in finally after return if we keep some statement those statement will be treated as dead code.



i)Return statement in finally block
  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  
  12. }
  13.  finally(){
  14.  return 1;
  15. }   
  16.  System.out.println("End of the method"); // Error : Unreachable code
  17. }
  18.     
  19.     
  20. public static void main(String[] args) {
  21.         
  22.         TryCatchReturn obj = new TryCatchReturn();
  23.        
  24.  
  25. }

  26. }
     
ii) return statement in finally



  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  

  9.  
  10. } catch (Exception e) {
  11.  
  12. }
  13.  finally(){
  14.  return 1;
  15. System.out.println("End of the method"); // Error : Unreachable code
  16. }   
  17.  
  18. }
  19.     
  20.     
  21. public static void main(String[] args) {
  22.         
  23.         TryCatchReturn obj = new TryCatchReturn();
  24.        
  25.  
  26. }

  27. }
     
iii) return statement in try catch and finally blocks



  1. package com.exceptionhandlingiinterviewquestions;
  2.  
  3. public class TryCatchReturn{
  4.  
  5. int calc(){ 
  6.         
  7. try {
  8.  
  9. return 10;
  10.  
  11. } catch (Exception e) {
  12.  return 20;
  13. }
  14.  finally(){
  15.  return 30;
  16. }   
  17.  
  18. }
  19.     
  20.     
  21. public static void main(String[] args) {
  22.         
  23.  TryCatchReturn obj = new TryCatchReturn();
  24.        
  25.  System.out.println(obj.calc())
  26. }

  27. }



Output:


  1. 30

finally with return statement in java

Remove specified element from Java HashSet example

1.Basic java example program to remove element hashset
  • boolean remove(Object o) .   This method is used to remove element from hashset if it is present it returns true.

  1. package com.getSizehashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.  hashSet.add(6);
  20.  hashSet.add(7);
  21.  hashSet.add(8);
  22.  
  23. System.out.println("Size of HashSet after addition : " + hashSet.size());
  24.  
  25. System.out.println("Hashset contains");
  26.  
  27. Iterator it=hashSet.iterator();
  28.              
  29. while(it.hasNext()){
  30. System.out.println(it.next());
  31.                      
  32. }   
  33.  
  34. boolean isRemoved = hashSet.remove(6);
  35. System.out.println("Was 6 removed from HashSet ? " + isRemoved );
  36.  
  37. System.out.println("HashSet after removal : " + hashSet);
  38. }
  39.  
  40. }
     


Output:

  1. Size of HashSet after addition
  2. 8
  3. hashset contains
  4. 1
  5. 2
  6. 3
  7. 4
  8. 6
  9. 7
  10. 8
  11. Was 6 removed from HashSet ?true
  12. HashSet after removal : [1, 2, 3, 4,5,7,8]
Select Menu