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

Monday, 24 February 2014

Difference between throw and throws

1. The key word throws is used in method declaration, this specify what kind of exception we may expect from this method. The key word throw is used to throw an object that is instance of class Throwable.

2. You can specify multiple exceptions thrown by a method using throws clause, where as throw throws one Throwable object only.

3. throw keyword can be used in switch statement, where as throws only in the method declaration.

Example
class ThrowEx{
 static void throwExceptions(int num){
  switch(num){
   case 1:
    throw new RuntimeException("Exception number 1");
   case 2:
    throw new RuntimeException("Exception number 2");
   case 3:
    throw new RuntimeException("Exception number 3");
   default:
    System.out.println("No Exceptions thrown");
  }
 }

 public static void main(String args[]){
  try{
   throwExceptions(1);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}
Output
java.lang.RuntimeException: Exception number 1
  

4. Throw keyword is used to throw exceptions from initializer block
import java.io.*;

class Stack{
 int size;

 {
  try{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter input");
   size = Integer.parseInt(br.readLine());
  }
  catch(Exception e){
   throw e;
  }
 }

 Stack()throws Exception{
 
 }

 public static void main(String args[]){
  try{
   new Stack();
   
   }
   catch(Exception e){
    System.out.println("Exception caught in main " + e);
   }
 }
}

Sample Output
Enter input
qwe
Exception caught in main java.lang.NumberFormatException: For input string: "qwe"


Note:
1. Static initializers cannot throw checked exceptions
2. Static initializers can throw unchecked exceptions like RuntimeException

throw clause                                                 try-resource statement                                                 Home

Friday, 21 February 2014

final variables

final is used to create two types of constants
    1. Creating final constants
    2. Creating final reference variables

1. Creating final constants.
Below post discussed about the final constants
http://selflearningjava.blogspot.in/2014/02/keyword-final.html

2. Creating final reference variables
When you assign an object to final reference variable, then the reference variable points to the same object, the object can change its state, I.e, its values. Assigning some other object or reference to the final reference variable is not allowed.

Example
class FinalReferenceEx{
  int width, height;

  public static void main(String args[]){
    final FinalReferenceEx ref1 = new FinalReferenceEx();

    ref1.width = 100;
    ref1.height = 200;
    System.out.println("Width is " + ref1.width + " height is " + ref1.height);

    ref1.width = 200;
    ref1.height = 300;
    System.out.println("Width is " + ref1.width + " height is " + ref1.height);
  }
}
   
Output
Width is 100 height is 200
Width is 200 height is 300

As you observe the output, the final reference variable is allowed to change the properties of the object that it is pointing. But it is not allowed to refer other object.

Example
class FinalReferenceEx{
  int width, height;

  public static void main(String args[]){
    final FinalReferenceEx ref1 = new FinalReferenceEx();

    ref1.width = 100;
    ref1.height = 200;
    System.out.println("Width is " + ref1.width + " height is " + ref1.height);

    ref1.width = 200;
    ref1.height = 300;
    System.out.println("Width is " + ref1.width + " height is " + ref1.height);

    ref1 = new FinalReferenceEx();
  }
}

I am trying to assign a new object to the final reference variable ref1, which is not acceptible, so compiler throws below error.

FinalReferenceEx.java:16: error: cannot assign a value to final variable ref1
ref1 = new FinalReferenceEx();
^
1 error

The same applicable for Arrays also, since Arrays also reference types.

Some Points to Remember

1. 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.

2. What is blank final variable in Java ?
Blank final variable in Java is a final variable which is not initialized while declaration, instead they are initialized on constructor or initializer blocks or static initialization blocks.

Related Links

final keyword                                                 final classes and methods                                                 Home