» » » Java Static Constructor : is it possible to create static constructor?

1.Is it possible to create static constructor in java?
  • No. We can not create constructor with static.
  • If we try to create a static constructor compile time error will come: Illegal modifier for the constructor
Static construcotr in java interview question



2.What is the real use of constructor in java? 

  • Constructors will be used to assign instance variables with default values.
  • Whenever object is created constructor will be called so the default values for the instance variables will be assigned in this constructor.
  • Top 15 Java Interview Questions on Constructors

  1. public class ConstructorDemo {
  2.  
  3.  int a,b;

  4. ConstructorDemo (int x, int y){
  5.  
  6. a=x;
  7. b=y;
  8.  
  9. }
  10. public static void main(String[] args) {
  11.  
  12.         ConstructorDemo ob= new ConstructorDemo ();
  13.  
  14.     }
  15. }


3. What is the use of static in java?
  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.
4.Is there any alternative solution for static constructor in java

  • Static means class level.
  • Constructor will be use to assign initial values for instance variables
  • static and constructor are different and opposite from each other.
  • To assign initial values for instance variable we use constructor.
  • To assign static variables we use Static Blocks
  • We can use static blocks to initialize static variables in java.


5.what is static block in java?

  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }
6. write a java program to assign static variables using static block


  1. package com.staticInitializer;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

  1.Static Variables 

  2.Static Methods 

  3.Static Blocks

  4.Main method

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Java Static Constructor : is it possible to create static constructor?

  1. Static constructor in java?? nice explanation :)

    ReplyDelete

Select Menu