Saturday, 31 January 2015
Invoking One Constructor from Another using this
Invoking One Constructor from Another
There is a specialized use of the this keyword that arises when a class has multiple constructors; it can be used from a constructor to invoke one of the other constructors of the same class. In other words, we can rewrite the two previous Circleconstructors as follows:
// This is the basic constructor: initialize the radius public Circle(double r) { this.r = r; } // This constructor uses this() to invoke the constructor above public Circle() { this(1.0); }
The this() syntax is a method invocation that calls one of the other constructors of the class. The particular constructor that is invoked is determined by the number and type of arguments, of course. This is a useful technique when a number of constructors share a significant amount of initialization code, as it avoids repetition of that code. This would be a more impressive example, of course, if the one-parameter version of the Circle() constructor did more initialization than it does.
There is an important restriction on using this(): it can appear only as the first statement in a constructor. It may, of course, be followed by any additional initialization a particular version of the constructor needs to do. The reason for this restriction involves the automatic invocation of superclass constructor methods, which we'll explore later in this chapter.
Friday, 30 January 2015
why java anonymous class variable declared final?
when an object of the anonymous class is instantiated, copies of the final local variables and method parameters referred to by the object's methods are stored as instance variables in the object.
The methods in the object of the anonymous class really access those hidden instance variables.
The methods in the object of the anonymous class really access those hidden instance variables.
Thus, the local variables and method parameters accessed by the methods of the local class must be declared final to prevent their values from changing after the object is instantiated.
Saturday, 3 January 2015
what is DispatcherServlet, RequestContextListener and RequestContextFilter in spring?
If you access scoped beans within Spring Web MVC, in effect,
within a request that is processed by the Spring
If you use a Servlet 2.4+ web container, with requests processed outside of Spring's DispatcherServlet (for example, when using JSF or Struts), you need to add the following
DispatcherServlet
, or
DispatcherPortlet
, then no special setup is
necessary: DispatcherServlet
and
DispatcherPortlet
already expose all relevant
state.If you use a Servlet 2.4+ web container, with requests processed outside of Spring's DispatcherServlet (for example, when using JSF or Struts), you need to add the following
javax.servlet.ServletRequestListener
to
the declarations in your web applications web.xml
file:<web-app> ... <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> ... </web-app>If you use an older web container (Servlet 2.3), use the provided
javax.servlet.Filter
implementation. The following snippet of XML configuration must be
included in the web.xml
file of your web
application if you want to access web-scoped beans in requests outside
of Spring's DispatcherServlet on a Servlet 2.3 container. (The filter
mapping depends on the surrounding web application configuration, so
you must change it as appropriate.)<web-app> .. <filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>requestContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app>
NOTE:DispatcherServlet
,
RequestContextListener
and
RequestContextFilter
all do exactly the same
thing, namely bind the HTTP request object to the
Thread
that is servicing that request. This
makes beans that are request- and session-scoped available further
down the call chain.
Subscribe to:
Posts (Atom)