Can consturctors have below modifiersa) finalb) voidc) static

This question is related to Oracle Interview

Showing Answers 1 - 7 of 7 Answers

Sunil

  • Sep 25th, 2006
 

In your case: constructor cannot have following modifiers1. Static2. FinalOne would get :ConstructorType.java:2: modifier final not allowed here final ConstructorType() { ^1 errorConstructorType.java:2: modifier static not allowed here static ConstructorType() { ^1 errorBut one can write void before the constructor name and Java does not give any compilation or run-time error as it treats it as any other method.Consider following example:class ConstructorType { void ConstructorType() { System.out.println("In ConstructorType"); } public static void main (String args[]) { System.out.println("Hello"); ConstructorType c = new ConstructorType(); }}void ConstructorType(), is treated as a method and when you create an instance of class ConstructorType, default constructor is called and not the method ConstructorType()

  Was this answer useful?  Yes

pramod.cbit

  • Mar 21st, 2008
 

no..construtors are meant for creating objects
 and it is not a method but loojk likes method.

1.so u cant put final to other than method,vaRIABLE ,class.

2.if u put satitic means u r breaking the concpet of objects...wherea s constructors are meant for objects

3.if u put void means u wont return anything...but constructors are expected to return its own calss type..but ompiler wont error u ..bcoz it treats it as a normal method with onstructor name.

  Was this answer useful?  Yes

sekhar511

  • Dec 10th, 2010
 

Constructors cannot have return types and modifiers where final, void, static are not allowed as modifiers to constructors.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions