Sunday, May 26, 2013

The final Keyword

final keyword in Java means "This cannot be changed".

It can be used in three places

final data
primitive: The value of primitive variable cannot be changed once initialized.
   - compile time constant:
 static final int MAX_CAR = 20;  
   - value initialized at run time:
 final int max_car = rand.nextInt(20);  
reference : This reference cannot point to another object once initialized.
 final String myStr = "Hello World";  

final methods
This method cannot be overridden, thus retain method's behavior during inheritance.

final classes
This class cannot be inherited, maybe for security or safety reasons.

@Override Annotation

Unlike C++, Java allows method overloading in inheritance.
For example, here we have three different versions of foo methods:
 class ParentOverload {  
      public void foo(int i) {  
           print("foo(int)");  
      }  
      public void foo(char c) {  
           print("foo(char)");  
      }  
      public void foo(float f) {  
           print("foo(float)");  
      }  
 }  

But if you intend to override a base class method but mistakenly typed an incorrect parameter, confusion arises because java treats the method as just another version of the same method instead of the one overriding the existing method.
 class ChildOverload extends ParentOverload {  
      // This foo(double d) is an overloaded version of other foo methods
      public void foo(double d) {  
           print("foo(double)");  
      }  
 }  

This is where @Override annotation comes to the use.

When you mean to override a method, you can choose to add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding.

 class ChildOverload extends ParentOverload {  
      @Override  
      public void foo(double d) {  
           print("foo(double)");  
      }  
 }  

compile error:
The method foo(double) of type ChildOverload must override or implement a supertype method

Sunday, April 7, 2013

Access Specifiers in Java

This article explains the difference between each Java access specifier and the effect when applied to class and class members.

Java's access specifiers, from least restrictive to most restrictive, are, public, protected, package access, and private.


Access Specifiers for Class Members
If a class member is public, any class in any package has access to it.
If a class member is protected, any subclass of that class in any package has access to it.
If a class member is declared package access, any class in the same package has access to it.
If a class member is private, only that class has access to it.

Access Specifier for Classes
If a class is public, any class in any package has access to it.
  • So this class provides service to clients.
If a class is declared package access, any class in the same package has access to it.
  • So this class acts as a helper class for other classes in the same package/library. Visible only to package/library owner.
Usually methods have the same access as the class.
Usually we won't use private and protected access specifiers for Classes.

Sunday, March 10, 2013

Order of Java Variable Initialization

This article outlines the execution order of the following Java variable initialization.
  1. Static Data Initialization
  2. Static Initialization Block
  3. Non-Static Data Initialization
  4. Non-Static Initialization Block
  5. Constructor
Rule 1: Class is Loaded
If the class is loaded for the very first time, 1 and 2 are executed depending on the textual order of variable initialization. The class is loaded if and only if one of the static variables or methods is accessed or if the class is created.

Rule 2: Class is Created
If the class is created, 3 and 4 are executed depending on the textual order of the variable initialization, then 5 is executed.

Class without inheritance example:
1:    class RaceCar {   
2:       int carColor = 0;  // Non-Static Data Initialization   
3:       static int maxTotalCar = 0;  // Static Data Initialization  
4:       static {  
5:          // Static Initialization Block  
6:          maxTotalCar = 20;  
7:       }  
8:       RaceCar() {   
9:          // Constructor  
10:         System.out.println("RaceCar()");   
11:      }   
12:      {  
13:         // Non-Static Initialization Block  
14:         carColor = 3;  
15:      }  
16:      static int minTotalCar = 0;  // Static Data Initialization   
17:   }   

1:  public static void main(String[] args) {  
2:    if(RaceCar.maxTotalCar == 1); // Rule 1 holds and line 3, 4 and 16 will execute  
3:    new RaceCar(); // Rule 2 holds and line 2, 12, and 8 will execute  
4:  }  

Class with inheritance example: 
Same rules apply but base class will be initialized before derived class.
Example:
1:  import static com.spotleo.util.Print.*;  
2:  class AutoMobile {  
3:       private int i = 9;  
4:       protected int j;  
5:       AutoMobile() {  
6:            print("i = " + i + ", j = " + j);  
7:            j = 39;  
8:       }  
9:       private static int x1 = printInit("static Insect.x1 initialized");  
10:       static int printInit(String s) {  
11:            print(s);  
12:            return 47;  
13:       }  
14:  }  
15:  class SUV extends AutoMobile {  
16:       private int k = printInit("SUV.k initialized");  
17:       public SUV() {  
18:            print("k = " + k);  
19:            print("j = " + j);  
20:            j = 57;  
21:       }  
22:       private static int x2 = printInit("static SUV.x2 initialized");  
23:  }  
24:  public class HondaCRV extends SUV {  
25:       private int l = printInit("HondaCRV.l initialized");  
26:       public HondaCRV() {  
27:            print("l = " + l);  
28:            print("j = " + j);  
29:       }  
30:       public static void main(String[] args) {  
31:            print("HondaCRV Constructor");  
32:            HondaCRV crv = new HondaCRV();  
33:       }  
34:       private static int x3 = printInit("static HondaCRV.x3 initialized");  
35:  }  
Output:
static Insect.x1 initialized
static SUV.x2 initialized
static HondaCRV.x3 initialized
HondaCRV Constructor
i = 9, j = 0
SUV.k initialized
k = 47
j = 39
HondaCRV.l initialized
l = 47
j = 57