ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Servlet is a java class. So, can there be a constructor in Servlet class or not? Why?

user-image
تم إضافة السؤال من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
تاريخ النشر: 2013/09/05
Mohammed Ameen
من قبل Mohammed Ameen , Java Developer , TATA Consultancy Services Limited (TCSL)

Yeah... we can have constructors in Servlets (the classes which implement javax.servlet.Servlet interface), but we generally don't have them for the following reasons:

 

1)Servlets are not instantiated explicitly unlike regular Java classes as they additionally require a special ServletConfig object for their initialization, which can only be created by the Servlet Container.

 

2)Servlet (javax.servlet.Servlet) is only an interface and not a class. Interfaces in Java don't have any constructors as it doesn't make sense for them to have one. And a constructor of an implementing class ,can of course not be declared in the Servlet interface for the obvious reason that we wouldn't be knowing the name of the implementing class(es).

Daanish Rumani
من قبل Daanish Rumani , Product Manager , Publicis Sapient

Servlet itself is an interface. So there is no question of it having a constructor. Known classes that implement the Servlet interface do have constructors but they do nothing as they do need to do anything.

Constructor for HttpServlet: http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServlet.html#HttpServlet()

المزيد من الأسئلة المماثلة