AddThis Social Bookmark Button

Static import is not new in Java. I avoided it till I could , but when you are asked "achieve more in less code" has always made me to tweak a little bit of code.But stuck infinitely in the dilemma of unreadable code. Java has always advocate of cleaner code (even though  in the cost of verbosity )

but the attempt of static import which made less verbose code look unreadable, And got into a problem which I would like to share with you guys.

Compare two arrays for equality. Note, this code below is just to demonstrate not the actual code I was trying out, but you will get the idea.

 

import static java.util.Arrays.*;

public class HelloWorld{

     public static void main(String []args){

        int a []={1,2,3};

        int b[]= {1,2,3};

        equals(a,b);

     }

}

 

Excelent , I could reduce the code , so now I don't have to repeat Arrays.equals() everytime I need this function, also , I had to use couple of other function from Arrays class , so the .* was needed, and worth. But I landed on a compiler error , guessed it ? Yes, that is correct.

 

error: method equals in class Object cannot be applied to given types;                                 
        equals(a,b);                                                                                                       
        ^                                                                                                                  
  required: Object                                                                                                         
  found: int[],int[]                                                                                                       
  reason: actual and formal argument lists differ in length  
 
 
I hope you liked what you saw.