Why cant we use static and final keyword for constructor?

Recommended Answers

All 5 Replies

why would you want to use those?
if you use static it'll go look for either a missing returntype, or a missing methodname.
let's say you could..

public class Person{
  private String name;

  public static Person(String newName){
    name = newName;
  }
}

just a few reasons why this is not even logical:
1. you can call a static method BEFORE an instantiation has taken place. the name variable doesn't exist untill the class is instantiated. so you are trying to set a value into a variable that doesn't exist yet.
2. even if it did... how is that "constructor" to know WHICH name to adjust? static means "class level", why name is on "instance level"

as for final:

final basically means: this method/class/member can not be overridden. but, by definition, a constructor is never overriden. it's being called by the subclasses not inherited, so there is no use for the final modifier.

A constructor makes an instance of the exact class in which it is declared. A constructor can never be overridden.

The "static" keyword means "not associated with any instance". So what would a static constructor do?

The "final" keyword means "do not allow overriding". So what would it mean for a constructor?

Constructors aren't inherited so can't be overridden so whats the use to have final constructor

When you set a method as 'static', it means: "Method belong to class, not belong to object" but constructor implicitly called when construct time of class, so what it is static?

Constructor is called automatically when an instance of the class is created, it has access to instance fields of the class. What will be the use of a static constructor.

Constructor can't be overridden so what will you do with an abstract constructor.

Constructor methods are a unique animal among other methods, scarcely resembling those other methods at all. They serve a specific purpose in Java programming.Normal methods are designed to hold and execute a group of Java code, regardless of how they do it. Constructor methods, however, exist only to instantiate the class they are in.In order to instantiate a class and create an object of that class, a call is made to that class's constructor method. For that reason, the constructor method must have the exact same name as its class, unlike typical methods.

commented: which does not mention static or final, so a lot of text, but not an actual answer to any part of the question. -3

thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.