Showing posts with label static blocks. Show all posts
Showing posts with label static blocks. Show all posts

Wednesday, 12 February 2014

Anonymous Classes

Anonymous classes are just like local classes, except these don't have name.

Syntax of Anonymous Classes
The syntax of an anonymous class expression is like the invocation of a constructor, except there is a class definition contained in a block of code.

Example   
Uniform zpschool = new Uniform(){
 String color;
 void setColor(String color){
  this.color = color;
 }

 String getColor(){
  return color;
 }
}; 


The anonymous class expression consists of the following:
       The new operator
     The name of an interface to implement or a class to extend.
     Parentheses that contain the arguments to a constructor

Creating Anonymous Classes using Abstract class
class AnonymousEx{
 abstract class Uniform{
  abstract void setColor(String color);
  abstract String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;
  
  void setColor(String color){
   this.color = color;
  }
  
  String getColor(){
   return color;
  }
 };

 public static void main(String args[]){
  AnonymousEx obj = new AnonymousEx();
  obj.zpschool.setColor("Blue");
  System.out.println(obj.zpschool.getColor());
 }
}

Output
Blue
 

Creating Anonymous class using Interface
class AnonymousEx{
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;
  
  public void setColor(String color){
   this.color = color;
  }

  public String getColor(){
   return color;
  }
 };

 public static void main(String args[]){
  AnonymousEx obj = new AnonymousEx();
  obj.zpschool.setColor("Blue");
  System.out.println(obj.zpschool.getColor());
 }
}

Output
Blue
1. Can I declare an interface inside Class ?
Yes

2. Can I declare abstract class inside class ?
Yes

3. Can I create an object for an Interface ?
Yes, Using Anonymous classes

4. Can I create an object for abstract class ?
Yes, Using Anonymous classes

5. An anonymous class cannot access local variables in its enclosing scope that are not declared as final
class AnonymousEx{
 String name="HI";

 interface Uniform{
  void setColor(String color);
   String getColor();
 }

 public void print(){
  int val = 10;
   
  class myClass implements Uniform{
   String color;

   public void setColor(String s){
    color = s;
   }

   public String getColor(){
    System.out.println(val);
    return color;
   }
  }
 }
}

When you try to compile compiler throws the below error
        
AnonymousEx.java:18: error: local variable val is accessed from within inner class; needs to be          declared final
System.out.println(val);
^
1 error

To make the program compiles fine, define the local variable as final
     final int val = 10;
  
6. You cannot declare static initializers or member interfaces in an anonymous class.
class AnonymousEx{
 String name="HI";
 
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 public void print(){
  class myClass implements Uniform{
   static String color;
   public void setColor(String s){
    color = s;
   }

   public String getColor(){
    return color;
   }
  }
 }
}


When you tries to compile the above program, compiler throws the below error

AnonymousEx.java:12: error: Illegal static declaration in inner class myClass
static String color;
^
modifier 'static' is only allowed in constant variable declarations
1 error

7. An anonymous class can have static members provided that they are constant variables

8. Can I declare extra methods in Anonymous classes ?
Yes. It is valid

Example
class AnonymousEx{
 interface Uniform{
  void setColor(String color);
  String getColor();
 }

 Uniform zpschool = new Uniform(){
  String color;

  public void setColor(String color){
   this.color = color;
  }

  public String getColor(){
   return color;
  } 

  void print(){
   System.out.println("I am Extra method");
  }
 };
}
 
As you see in the above program, interface Uniform has 2 abstract methods, setColor, getColor.But the Anonymous class has extra method print().


9. Is below program compile ?
class AnonymousEx{
 abstract class Uniform{
  void setColor(String color);
  String getColor();
   }
}
No, Compiler throws the below error.
AnonymousEx.java:4: error: missing method body, or declare abstract
void setColor(String color);
^
AnonymousEx.java:5: error: missing method body, or declare abstract
String getColor();
^
2 errors

To solve the above error, make the methods in the abstract class as abstract

class AnonymousEx{
 abstract class Uniform{
  abstract void setColor(String color);
  abstract String getColor();
 }
}
         

Local Classes                                                 Lambda Expressions                                                 Home

Sunday, 9 February 2014

keyword : final

final is used to define constants. The final modifier indicates that the value of this field cannot change.

Defining class level constants
Declare the field with final and static combination

Example
class Person{
 static final int MAX_SALARY = 10000000;
 public static void main(String args[]){ 
  System.out.println(Person.MAX_SALARY);        
 }
}
       
Output
10000000
   
Defining Object level constants
    Declare the field with final keyword.

   Example
class Person{
 final int MAX_SALARY;

 Person(){
  MAX_SALARY = 10000;
 }

 Person(int sal){
  MAX_SALARY = sal;
 }

 public static void main(String args[]){
  Person p1 = new Person(1000000);
  System.out.println(p1.MAX_SALARY);
 }
} 

Output
 1000000 
   
Some Points to Remember
1. You must initialize the static final variables, other wise compiler throws the error
Example
class Person{
 final static int MAX_SALARY;
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
  

When you try to compile the above program, compiler throws below error
Person.java:1: error: variable MAX_SALARY might not have been initialized
class Person{
^
1 error

This is not the case with non final variables.
Example
class Person{
 static int MAX_SALARY;
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}

Output    
0
          
2.Static final variables must be initialized at the time of creation or in the static block, Otherwise compiler throws error
class Person{
 static final int MAX_SALARY;
 
 static{
  MAX_SALARY = 1000000;
 }
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
  
Output
1000000
   

Above program compiles and runs fine, since initializing the static final variable in the static block is completely fine.

3. Trying to initialize the static final variable other than at the time of creation or in static block cause the compile time error. 
Example
class Person{
 static final int MAX_SALARY;
 
 Person(){
  MAX_SALARY = 1000000; //is a static final, can't be initialized here
 }
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
        
 When you try to compile the above program, compiler throws the below error
Person.java:5: error: cannot assign a value to final variable MAX_SALARY
MAX_SALARY = 1000000;
^
1 error

4. You must initialize the final variables whether those are static or non-static, otherwise compiler throws error
class Person{
   final int MAX_SALARY;
   public static void main(String args[]){
   }
}


When you tries to compile the program, Compiler throws below error
Person.java:1: error: variable MAX_SALARY might not have been initialized
class Person{
^
1 error
                           
5. Final instance variables must be initialized in the constructor or in the instance blocks.
Example 1
class Person{
 final int MAX_SALARY;
 Person(){
  MAX_SALARY = 10000;
 }
}
     
Example 2  
class Person{
 final int MAX_SALARY;
 
 {
  MAX_SALARY = 100000;
 }

 Person(){

 }

 void setSalary(){

 }
}

6. Trying to initialize final instance variables, other than in constructor causes compile time error.  

class Person{
 final int MAX_SALARY;

 Person(){
 }

 void setSalary(){
  MAX_SALARY = 100000;
 }
}
  

Compiler throws the below error
Person.java:8: error: cannot assign a value to final variable MAX_SALARY
MAX_SALARY = 100000;
^
1 error
 
7. final for Reference types: For references to objects, final ensures that the reference will never change, meaning that it always refer to the same object. It makes no guarantees whatsoever about the values inside the object being referred to staying the same.

8. How to declare global constants in Java ?
Make a variable as public static final.
                    


this keyword                                                 nested classes                                                 Home