why constructor cannot be final?

whhy we cannot use final keyword for a constructor

  • Soumya B
  • Soumya B
  • |
  • 21 May
  • |
  • 13830 Views
  • |
  • 47 Answers

text limit upto 4000

  • 1

    as final makes any method unable to change it's parameters. so now imagine you call a class multiple times in your programs assigning different values for initialization. then multiple references are called of constructor but since it is final all references possess same identity so it throws a runtime error

    • Voted By:
    • Amit

  • 1

    constructor can never be inherited. So you can't need to use final keyword.

    • Voted By:
    • Revuri

  • 0

    if you make constructor as final. creating object is not possible .
    we can make constructor as final in singleton design pattern concept.

    for example:
    public class SingletonExample
    {
      private static SingletonExample se=null;
      private SingletonExample(){} // private constructor
    public static SingletonExample getInstance(){
            if(se==null){
              se=new SingletonExample();
          }
    return se;
    }

    public class Testing{
     public static void main(String args[]){
    SingletonExample sm1=new SingletonExample(); // compile time erro like The constructor of SingletonExample() is not visible
          SingletonExample sm1=SingletonExample.getInstance();
         SingletonExample sm2=SingletonExample.getInstance();
         SingletonExample sm3=SingletonExample.getInstance();
       System.out.println(sm1+" : "+ sm2+" : "+sm3);
    }
    }

    output:
    SingletonExample@4a923e34
    SingletonExample@4a923e34
    SingletonExample@4a923e34

    same output will get because SingletonExample class constructor is final we can not create object for final constructor object.
     


  • 0

    Constructor cannot be final because a constructor initiatizes variables for objects of class which will later be changed by the object


  • 0

    the easy answer is that you can think or consider about constructor that is "special method", it is a java language rule that made this method is constructor. also, because of the constructor can't be overriden. so that's useless to make the costructor method can be final. because the (final) used to prevent the override, and the contructor can't be overriden by defualt. hopefully it is clear

     


  • 0

    A constructor is never inherited to an another class so it doesn't need to be marked final.


  • 0

    Hey,
    The constructor cannot be final because the constructor cannot be inherited and in constructor values are initialized to the variable so by this variables changes every time and yes it cannot be inherited.


  • 0

    I think JVM calls cumstructor when we want tocreate an object of the class. We create objects for allocating memory for data members and variables. Once we make cumstructor as final we cant do modification as per final property. So we cant assume that every time create an object the memory will b same for each call. Thats y we cant make constructors as final.


  • 0

    constructor cannot be inherited
     


  • 0

    I think this becuase constructor is used for initialisation of variables, so for any class initialisation should be public access and able to be editing so no final.


  • 0

    A constructor is never inherited to an another class so it doesn't need to be marked final. Also it cannot be marked final.


  • 0

    apply a final keyword to a method means you cannot override that method. but by default in java no constructor can be overridden. so there is no need to use final keyword with a constructor .


  • 0

    a constructor can't be inherited so can't be override, so no use of making final


  • 0

    A constructor can't inherited so can't be overridden , so there is no use of making constructor final.


  • 0

    We can't make a constructor to be final because the constructor is never inherited.


  • 0

    no it can not?


  • 0

    Contructor cannot be inherited, so no sense of declaring final.


  • 0

    Constructor can not declared as final because it cannot be inherited


  • 0

    constructor cannot be inherited so we cannot use final keyword


  • 0

    in constructor only access modifier public, private, protected & default allowed.


Did Not Get The Answer? 
Contact people of Talent-Core Java directly from here