No, it’s not. In reflection both extend
Constructor does not extend
Method. It is a common misconception that constructors are methods and often asked about at job interviews.
The Java® Language Specification
The JLS always knows best. Methods and constructors are always two separate things in the JLS.
Special Methods (Bytecode):
The last one is interesting because in the bytecode a constructor is actually an instance initialization method, which is a special method. So only at bytecode level a constructor is a method. But Later, when you use reflection at runtime, it is not a method.
Constructor is not a
Method ctor = String.class.getConstructor(); => Type mismatch: cannot convert from Constructor<String> to Method
For some reason the method java.lang.Class.getConstructor(Class<?>…) throws a NoSuchMethodException when no matching constructor was found. There should have been a
A Constructor is a Function, which is a kind of Method Reference:
Function<Integer, int> ctor = int::new; IntFunction<int> fn = int::new;
But this is only true in functional programming. In Java OOP is much more important than FP, which was only added in Java 8.
How Constructors work
Only by the use of the
new operator it is possible to execute a constructor. But first the JVM has to allocate memory for the new object.
Each constructor will have (explicitly or implicitly) a call to the super-constructor as the first statement. This goes up until Object() is reached, which can’t call a super-constructor (it’s the top of the hierarchy). When super is finished all (nonstatic) members are initialized. Only then the constructor is executed. Initialization blocks are compiled into the constructor, so they are executed as well. The JVM then returns to where
new was used and returns a reference to the newly created object.
The exact procedure is hard to understand because so many blocks of code and expressions are executed. And this is a lot different from a simple call to a method. But you can use a debugger to trace the exact process.
When assigned to volatile Field
A method can be
synchronized or not. But a constructor isn’t. However, you can use a sync-block on the class inside the code block of that constructor. Usually that’s not necessary. But you can get problems when multiple threads are accessing a field and you create a new instance to assign to that field. Then Java will automatically execute the constructor first and only then assign the new reference to that volatile field. No thread could see a partially constructed object. When using a nonvolatile field you could have one thread construct a new object and another thread already using it, which would end in chaos.
Properties of Methods and Constructors
- Return something / are void
- You can override a method
- Are bound to an existing object, unless
- Can call super()-implementation
- Can not set final fields
- Can be
- Can be invoked as a method
- Used to initialize a newly created object
- Are not inherited
- You can’t override a Constructor
- Are not bound to an existing object
- Can call super()-constructor
- Can set final fields
- Are not
- Can only be invoked with
- Have a
- Can call methods on
- Can throw exceptions
- Can have parameters
- Can use the