What is the difference b/w static block and constructor

Discussion in 'Object Oriented Concepts' started by chsrinu, Mar 29, 2010.

  1. chsrinu

    chsrinu New Member

    hai friends,

    what is differnce b/w static block and constructor in a class
     
  2. chandrakanth.amuru

    chandrakanth.amuru New Member

    Re: what is difference b/w static block and constructor

    The static block will be executed when the class is loaded on to the JVM. It is executed only once.
    Constructor will be executed while creating the object. The constructor is called only once for an object and it is called as many times as you create the object.
    Ex: if a class Test is having one static block and a constructor. Then the static block is executed only once. If you create three objects based on Test class then the constructor is called three times.
     
  3. jagsjava

    jagsjava New Member

    The main difference is the static block don't need any object creation for execution. but the constructor to execute we have to create an object.
    i.e The constructor gets automatically executed when the object is created.
     
  4. dineshkumaronmail

    dineshkumaronmail New Member

    Static blocks are executed before running the main method. where as constructor are called while creating an object of an class..

    Public Class test{
    static{
    System.out.println("welcome");
    }
    }

    if we above run the above program it will print welcome.
     
  5. parulmahajan

    parulmahajan New Member

    if you want something constant that is independent of object then keep it in static block and other initializations that are required to be object specific keep it in constructor.

    coz constructors runs every time an object is created but static block runs only once when class is loaded to JVM.
     
  6. mahesh.cha86

    mahesh.cha86 New Member

    Before going to the main method jvm serch for the static block if it finds the static block the it execute first while creating the object constructor is used to intilise values of object
     
  7. venkatjdv

    venkatjdv New Member


    Constructor is used to create an object for the class at runtime.But static blocks are used to execute the block with out main().

     
  8. kamalakarjava

    kamalakarjava New Member

    The Code inside the Static Block is executed at class loading time.and This occurs only once.But the code inside the constructors is executed at RunTime ,for each and every object instantiation of that class.
    So, Static Blocks are suggestable when there is a requirement of code to be executed only once. and code is not at all specific to object of that class.
     
  9. ravikumar4199

    ravikumar4199 New Member

    you go on to brief explanation to
    public static void main(-)
    it is easyly understood.
     
  10. anneroberts

    anneroberts Member

    A static square, or static instatement piece, is code that is run once for each time a class is stacked into memory. ... The technique sets up the class. You can make over-burden constructors. These are worked with an indistinguishable name from the class however with various parameters.
     

Share This Page