Showing posts with label final variables. Show all posts
Showing posts with label final variables. Show all posts

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

final keyword

final key word is used to define

Java support the creation of constants using final keyword.

super keyword                                                 final variables                                                 Home

Sunday, 16 February 2014

Interfaces in Java

In JAVA, interfaces are reference types contain only constants, method signatures, nested types.

Interfaces doesn't contain method bodies, these are implemented by classes, extended by interfaces.

Defining an Interface
   interface InterfaceName{
      // constant declarations, if any
      // method signatures
      // Nested Types
   }

Note
All the methods in the interface are public.
All the variables in the interface are public static final.
Implementing An Interface
A class implements the interface provides the method body for the method signatures in the interface.

Syntax
    Class ClassName implements Interface1, Interface2, ...Interface N{
        //implements the interfaces
    }

Example
interface Circle{
  double PI = 3.1428;
  double getArea(int r);
}

 
class MyCircle implements Circle{
  public double getArea(int r){
    return (Circle.PI * r * r);
  }

  public static void main(String args[]){
    MyCircle circle1 = new MyCircle();
    System.out.println("Area of the circle is " +circle1.getArea(10));
  }
}


Output 
Area of the circle is 314.28
 
Some Points to Remember

1.If a class implements an interface, then it must implement all the methods in the interface, other wise, the class must declared as a abstract.
Example
interface Circle{
  double PI = 3.1428;
  double getArea(int r);
}

class MyCircle implements Circle{

}
   
When you tries to compile the class MyCircle, compiler throws the below error
MyCircle.java:1: error: MyCircle is not abstract and does not override abstract
method getArea(int) in Circle
class MyCircle implements Circle{
^
1 error

To make the program compile,there are two options
    1. Implement the methods signatures of the interface in the class.
   2. Make the class as abstract like below
        abstract class MyCircle implements Circle{

        }
  
2.By default all the methods in the interface are public, while implementing the interface, class must provide the public access specifier for the methods it implementing, otherwise “weaker access privileges” error thrown at compile time.

Example
interface Circle{ 
  double PI = 3.1428;
  double getArea(int r); 
}

class MyCircle implements Circle{
  double getArea(int r){
    return (Circle.PI * r * r);
  }
}

When you tries to compile the MyCircle class, compiler throws the below error.

MyCircle.java:2: error: getArea(int) in MyCircle cannot implement getArea(int) in Circle
double getArea(int r){
^
attempting to assign weaker access privileges; was public
1 error


3. All the variables in the interface are final by default, so updating the variable in a class causes the compile time error
Example
class MyCircle implements Circle{
  public double getArea(int r){ 
    Circle.PI=3.12; 
    return (Circle.PI * r * r);
  }
}

When you tries to compile the above program, compiler throws the below error
MyCircle.java:3: error: cannot assign a value to final variable PI
Circle.PI=3.12;
^
1 error


4. Can interface has static methods ?
Yes (From Java8 onwards)



Interfaces                                                 Interface as reference type                                                 Home