Monday, August 18, 2014

Shopizer the First Step

As I love programming, I devote myself to be a volunteer programmer in shopizer project, it's an open source project that create an e-commerce application using Java ecosystem (With mainly Spring MVCSpring CoreSpring SecurityHibernateJBoss InfinispanElasticsearch and JBoss Infinispan).

However as the first step to be a part of the development team you should be able build the application locally and run it successfully, but I encounter a problem in this stage and maven wasn't able to deliver the war file from the core project "sm-core" which is necessary to run the main one "sm-shop",  in addition it displays an error message :

--> [INFO] BUILD FAILURE

My desktop while building Shopizer!

(Using My Maven version is : 3.2.2 and my Java : 1.8 Oracle version.)

...Yes that's was a mess especially by attempting a couple of actions to make thing in its place, but without a tangible result,
...by the way and by analyzing the file which was created from the mvn command using pipe in my Ubuntu, I realize that there's a warning indicates that the supported source version for java is 1.6 for some sources but I'm using 1.8, that was not clear as it's  thought but it was evident for java daily developer that maven talk about Java version and not about something else.

So I switched to the oracle JDK 1.6, hence  the building successfully passed and I was able to create my war file of the core project, and  by following the remaining instructions I've run the shopizer app without any issue, which mark my successful beginning in the dev team......Yoppi

-->[INFO] BUILD SUCCESS


What I've learnt from this issue??
I learned that before building a project or running it I should read carefully the related instructions and I should be able to use any accessible debug tool effectively (Use Pipe technique to generate a log file, Use different options and let your command to be more verbose...   ) because it's not the matter of what you know but the matter is how you USE what you know.....



Wednesday, August 6, 2014

The Final keyword for Variable



I always heard from other java developers that a Final variable is a constant that will never “change” its value, like:
Final int pi= 3,14 ;

...Then if we try to change the [pi]  value we will get a compiler error :
Pi=0 // compiler error -> "The final local variable i cannot be 
        assigned. It must be blank and not using a  compound 
        assignment"

That’s true  for primitive type in java but what’s about the variable that point to a reference,  does the object will really never change?

Let’s see the definition from the source :
“Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. »

So as text says, if the variable HOLDS a reference to an object, the behavior will not be as like as a primitive type, …..Hmm…that’s interesting…let’s do some practicing to unveil this concept. 

Note : Don't waste your time by copy and paste the code, you can download the whole project from the link at the end of the article (in the very last sentence!!!!).

We create [Person] class as follow:

public class Person {
  String lastName;
  String firstName;
  int age;
 
  public Person(String lastName, String firstName, int age)
  {
     this.lastName = lastName;
     this.firstName = firstName;
     this.age = age;
  }
}
In the source file you'll find more code in the [Person] class(Getters, Setters...)

The main class:
public class MainApp {

 public static void main(String[] args)
  {
     final Person p1;
     final Person p2 = new Person("Ahmed", "SAMIR",20);
     p1 = new Person("Omar", "BEHLOUL",38);

     p1.age = 26;                 // ok no error
     p2.firstName = p1.firstName; // coz we're changing the fields and not the reference

     // p1 = p2; // compiler error --> "The final local variable p1 may already have been assigned"
                 // p1 can't refer another object (p2)
  }
}

….So as we can see the final keyword bring a tricky behavior when the variable points to reference : 
  • You can’t refer it to another object after the first assignment.
  • The member/fields of the final variable can be freely changed without any restriction.
                    ...may be one day Java can close this back door of final keyword with variable!


However this attitude of final variable can be a benefit for your program but can be against you too , and it mainly depends on your designing of the application...so be careful!!

Hey...one minute, just to remember you that final keyword exists also for:
  • Class to stop inheritance. 
  • Method to stop overridden.
that's all......

You can download the source code from here...Good testing!