Thursday, May 2, 2013

Java / Inner classes


Inner classes.
There are two types of inner classes:
- static (static nested classes)
- non-static (inner classes)

Non-static nested classes have access to other members of the enclosing class,
even if they are declared private.
Static nested classes don't have access to other members of the enclosing class.

There are few reasons for using nested classes.
- logical grouping classes used in one place
- increasing encapsulation
- more readable and maintainable code

public class MyNested 
{
 private int x = 10;
 private int y = 15;
 
 public void showAddition()
 {
  Operation o = new Operation();  
  System.out.println( o.add() );
 }
 
 class Operation
 {
  private int add()
  {
   System.out.println( "Adding" );
   return x+y;
  }
 }
 
 // ! 
 // Static nested classes don't have access to members of enclosing classes 
 static class StaticOperation
 {
  private int add()
  {
   return x+y;
  }
 }
 
 public static void main(String[] args) 
 {
  MyNested n = new MyNested();
  n.showAddition();
  
  // To instantiate an inner class, you must first instantiate the outer class. 
  // Then, create the inner object within the outer object 
  MyNested.Operation op = n.new Operation();
  op.add();

 }
}

Java / Initialization of variables


How can we initialize fields?
There are few ways to do it.

1. Initialization in its declaration - but sometimes we need some logic to do that.
2. Initialization in static block
3. Initialization in block
4. Initialization in constructor
5. Initialization by method

public class MyInitialization 
{
 static int a, b;
 int x, y;
 String name;
 String address;
 
 
 // 1. initialization in its declaration 
 int age = 30;
 
 // 2. initialization in static block 
 static
 {
  a = 7;
  b = 9;  
 }
 
 // 3. initialization in block 
 {
  x = 21;
  y = 23;  
 }
 
 // 4. initialization in constructor 
 MyInitialization()
 {
  this.name = "John";
 }
 
 // 5. initialization by method 
 public void setAddress(String aAddress)
 {
  this.address = aAddress;
 }
 
 public void setX(int aX)
 {
  this.x = aX;
 }

 public static void main(String[] args) 
 {  
  MyInitialization mt = new MyInitialization();
  
  System.out.println( mt.age );  // 1
  
  System.out.println( MyInitialization.a ); // 2
  System.out.println( MyInitialization.b ); // 2
  
  System.out.println( mt.x ); // 3
  System.out.println( mt.y ); // 3
  
  System.out.println( mt.name ); // 4
    
  mt.setAddress("London"); // 5
  System.out.println( mt.address );
 }
}

Java / Enum type


Enum type is a data type that enables for a variable to be a set of predefined constants.
The variable must be equal to one of the values that have been predefined for it.
Because they are constants, the names of an enum type's fields are in uppercase letters.
enum WeekDays 
{
 MONDAY, THUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

public class MyEnum
{
 WeekDays weekDays;
 
 public MyEnum(WeekDays aWeekDays) 
 {
  this.weekDays = aWeekDays;
 }
 
 public void showCommentForEachDay()
 {
  if( weekDays.equals(WeekDays.SATURDAY) || weekDays.equals(WeekDays.SUNDAY) )
   System.out.println("I like weekends");
  else
   System.out.println("Waiting for weekend... it's " + this.weekDays);
 }
 
 public static void main(String[] args)
 {
  MyEnum me = new MyEnum(WeekDays.SATURDAY);
  me.showCommentForEachDay(); /* out: I like weekends */
 }
}

Sunday, April 28, 2013

Java / break and continue statements


Difference between break and continue statement.
- break - loop is terminated
- continue - stops the normal flow of loop and returns to the loop without executing statement after the continue statement.

public class MyBreak 
{
 public static void main(String[] args) 
 {
  for(int i=0; i<30; i++)
  {
   System.out.print(i);
   
   if(i == 10)
   {
    System.out.println(" \nBreaking the loop when i == 10 ");
    break;    
   }
   System.out.println(" NEXT...");
  }
  System.out.println("End of first loop\n");
  
  for(int i=0; i<30; i++)
  {
   System.out.print(i);
   
   if(i == 10)
   {
    System.out.println(" \nContinue statement when i == 10");
    continue;    
   }
   System.out.println(" and still iterating...");
  }
 }
}


As you can see break statement terminated loop when i == 10.
Continue statement only terminated flow when i == 10 and returned to the iteration.