Tuesday, September 23, 2014


Interface can have method definitions too (java 1.8 ):
 
Explanation from Oracle :
 
An example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-controlled car manufacturers add new functionality, such as flight, to their cars? These manufacturers would need to specify new methods to enable other companies (such as electronic guidance instrument manufacturers) to adapt their software to flying cars. Where would these car manufacturers declare these new flight-related methods? If they add them to their original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. If they add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
Few important points.

  • To make a method as default method it must be started with the keyword default
  • You need not to override default method in the implementation class.
  • Default methods are by default available in the implementation class and can be invoked using an object of implementation class.

A simple example
//interface DefaultMethod.java
interface DefaultMethod {
      
       //abstract Method
       public void methodA();
      
       //static methods
       static void methodB()
       {
              System.out.println("Inside Method B");
       }
      
       //default Method
       default void newDefaultMethod()
       {
              System.out.println("\n Default Method");
       }
}
//class DefaultMethodImplementation.java which implements DefaultMethod interface.
public class DefaultMethodImplementation implements DefaultMethod {
      
       public void methodA(){           
              System.out.println("\n Inside A");      
       }
       public static void main(String[] args) {       
              DefaultMethodImplementation dmi=new DefaultMethodImplementation();  
              dmi.methodA();
              dmi.newDefaultMethod();          
       }
}

Wednesday, November 28, 2012

Volatile Vs Non-Volatile Variables in Java ???

If you are working with the multithreaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it’s updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn’t know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value.

Declaration :
                  private volatile int x;

Thanks,

Interceptors Vs Filters???

1) The most significant difference is that "interceptors" are a part of the Struts 2 framework, and are only part of requests handling that is done by the Struts 2 framework.
 "Filters" on the other hand are a part of the Servlet Specification in other words, they are part of the Servlet API. If you are using Struts 2, you should user interceptors for wrapping functionality around your Struts 2 actions. If you are trying to wrap functionality around requests coming to your web application, but not being handled by Struts 2, then a filter might be more appropriate.

2) The Interceptor stack fires on requests in a configured package while filters only apply to their mapped URLs.

3)Interceptors can be configured to execute or not depending on specific target action methods via excludeMethods and includeMethods while Filters lack this feature.

Filters are an implementation of the Intercepting Filter pattern while Interceptors are of the Interceptor pattern.

Thanks,

Integration of EXTJS with Struts 2

We need to just keep Extjs files in the page and the name of the components should be the same as the name of the attributes in Action so that we can refer those for the sake of validation.
In Struts 2.0 and above,We just need to make a single xml instead of two in Struts 1.x,with the name "ActionClassName-validation.xml" and this must be kept in the class path of the application.
To display error messages in view page,we can use <s:fielderror/>.

Thanks,