Thursday, June 28, 2012

Core Java Tips and Tricks


  1.    public class HelloWorld {  
  2.    
  3.   
  4. /** 
  5.  * @param args 
  6.  */  
  7. public static void main(String[ ] args) {  
  8. System.out.println("Sam!");  
  9.    
  10. }  
  11.   
  12. }   


This is a very simple piece of code and everyone of you must be know the output of this simple code. Yeah you are right! 

Output : Sam!

But what if i want to show "Hi Sam! How are you?"  without changing in the main method. The first thing came in my mind is a static block of code. So there are many ways to do the same. like for example :


  1. static {  
  2.     System.out.println("Hi Sam! How are you?");  
  3.     System.exit(0);  
  4. }   

The above code will surely Print what i want to see, but what if i want to use main method too. For that we have an another option too. lets have a look at that:

  1. static {  
  2.     System.out.print("Hi ");  
  3.     Runtime.getRuntime().addShutdownHook(new Thread() {  
  4.         public void run() {  
  5.             System.out.println(How are you?");  
  6.         }  
  7.     });  

This code is quite more tricky as it deals with the multithreading. 
and addShutdownHook(Thread hook) -->Registers a new virtual-machine shutdown hook. A shutdown hook is simply an initialized but unstarted thread. 
So the above code first print "Hi" and then calls the unstarted thread of the program that is the main method of class and then it print "How are you?" through println function. 
Runtime.getRuntime() - Returns the runtime object associated with the current java application.

Now come to the real trick, in which we will change the default behavior of println() function.

Here is the code:



  1. public class HelloWorld  
  2. {  
  3.     // Static initializer; this is called when the class is loaded,  
  4.     // and before the main method is executed.  
  5.     static  
  6.     {  
  7.         // Get a reference to the current output stream  
  8.         final PrintStream out = System.out;  
  9.         // Create a new anonymous subclass of PrintStream.  
  10.         // This class inherits all methods from the regular PrintStream class,  
  11.         // including all print and println methods  
  12.         PrintStream newOut = new PrintStream(out)  
  13.         {  
  14.             // Override println(String) which will be used in the main method  
  15.             public void println(String s)  
  16.             {  
  17.                 // Using the default print method, print "Hai " first, then s  
  18.                 // When called from main, s will be "Sam!"  
  19.                 print("Hi ");  
  20.                 print(s);  
  21.                 // Call super.println, which is the implementation from PrintStream  
  22.                 super.println(" How are you?");  
  23.             }  
  24.         };  
  25.         // Set System.out to the new PrintStream  
  26.         System.setOut(newOut);  
  27.     }  
  28.   
  29.     /** 
  30.      * @param args 
  31.      */  
  32.     public static void main(String[ ] args)  
  33.     {  
  34.         System.out.println("Sam!");  
  35.     }  
  36. }  



So this is the way to override the println() function. you can do the trick with other functions of PrintStream class.