Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Can we declare a constructor inside a method in java?

user-image
Question added by Anand Kumar , Trainee , globysn Skills ,kolkata
Date Posted: 2013/11/16
Advaitha Chidurala
by Advaitha Chidurala , Teacher , Little Masters Abacus

You can only create a constructor with in a class whose name is same as the class name, you cannot definre it inside a method. 

Y to add constructor. According to java when you declare a function or method, a constructor is automatically derived. So if u try to add it would be overloaded.

 

it shows error. So its not possible.

Abdul Majid Gaho
by Abdul Majid Gaho , Sr Java/Angular developer , Ministry Of Health - Oman

No, it is not possible.

HATIME ENHAS
by HATIME ENHAS , Ingénieur Etudes et Développement , Atos ITS Nearshore Centre Maroc

Yes you can create it if you have an inner class inside your method.

Shailender Kumar
by Shailender Kumar , Software Instructor , Markaz-E-Adab-O-Science

Yes u can create

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.