Working of static blocks, final variables




                        http://improvejava.blogspot.in
                                                         1
Objective


• On completion of this period, you would be able to know


              • Static blocks

              • Final variables




                            http://improvejava.blogspot.in
Recap

• When objects of its class are declared, no copy of a static variable
  is made

• Instead, all instances of the class share the same static variable

• When a member is declared static, it can be accessed before any
  objects of its class are created, and without reference to any object

• The most common example of a static member is main()




                          http://improvejava.blogspot.in
                                                                          3
Recap                 contd..


• Outside of the class in which static methods are defined, static
  methods and variables can be used independently of any object.


• To do so, you need only specify the name of their class followed
  by the .(dot) operator

                   ClassName.methodName()




                         http://improvejava.blogspot.in
                                                                     4
Static Blocks
• Static block is a block of code prefixed by ‘static’ keyword
• Gets executed only once
• Used to initialize static variables

class A {
      static int x;
      static {                               static block
          x = 10;                             initializes
      }                                         x to 10
      public static void main (String args[]) {
        System.out.println (“ x value is : ”+ x);
      }
}
Output : x value is : 10
                           http://improvejava.blogspot.in
                                                                 5
Example Program : Static Block
class A {
         static int x = 10;
         static int y;
         static void call( int p) {
                   System .out. println(“ x value is :”+x);
                   System .out .println(“ y value is :”+y);
                   System .out .println(“ p value is :”+p);
         }
         static {
                   System.out.println(“ static block initialized”);
                    y=a*2;
         }
         public static void main (String args[]) {
                   call(30);
                                                         Output
         }                                                      x value is : 10
}                                                               y value is : 20
                                                                p value is : 30
                            http://improvejava.blogspot.in
                                                                                  6
Final Variables
• A variable can be declared as final

• Doing so prevents its contents from being modified

• This means that you must initialize a final variable when it is
  declared

• Permits us to create typed constants

• In usage, final is similar to const in C / C++


                      http://improvejava.blogspot.in
                                                                    7
Example final variables


Syntax
         final type constName = value;

Example
      final int FILE_NEW = 1;
      final float PI = 3.141519;




                   http://improvejava.blogspot.in
                                                    8
Final Variables

• Subsequent parts of the program may use the above final
  variables FILE_NEW and PI

• It is common coding convention to choose all uppercase
  identifiers for final variables

• Variables declared as final do not occupy memory on a per-
  instance basis

• Thus, a final variable is essentially a constant


                       http://improvejava.blogspot.in
                                                           9
Example program : final variables
             Correct program                                 Wrong program

class A {                                         class B {
    final int X = 10;                                 final int X = 10;
    public static void main (String args[])           public static void main(String args[]) {
{                                                           X = 20;
          System.out.println(“ X is “+X);                   System .out. println(“ X is “+X);
    }                                                 }
}                                                 }


     final variable
     should not change




                                 http://improvejava.blogspot.in
                                                                                           10
Other Uses Of final


• The keyword final can also be applied to methods,
• Its meaning is substantially different than when it is
  applied to variables




                   http://improvejava.blogspot.in
                                                           11
Summary


• Static blocks
• Final variables




                    http://improvejava.blogspot.in
                                                     12
Assignment


1. Write a class that implements static blocks

2. Write a class that uses final variables




                     http://improvejava.blogspot.in
Quiz
1. Static blocks are executed
   a. Java program is compiled
   b. Only one time
   c. Several times
   d. None




                   http://improvejava.blogspot.in
                                                    14
Quiz                         contd..

2. Final variable is
   a. variable
   b. instance variable
   c. constant
   d. static variable




                   http://improvejava.blogspot.in
Frequently Asked Questions



1. Explain the use of static blocks

2. Explain the use of final variables




                     http://improvejava.blogspot.in
                                                      16

Static blocks, final variables .19

  • 1.
    Working of staticblocks, final variables http://improvejava.blogspot.in 1
  • 2.
    Objective • On completionof this period, you would be able to know • Static blocks • Final variables http://improvejava.blogspot.in
  • 3.
    Recap • When objectsof its class are declared, no copy of a static variable is made • Instead, all instances of the class share the same static variable • When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object • The most common example of a static member is main() http://improvejava.blogspot.in 3
  • 4.
    Recap contd.. • Outside of the class in which static methods are defined, static methods and variables can be used independently of any object. • To do so, you need only specify the name of their class followed by the .(dot) operator ClassName.methodName() http://improvejava.blogspot.in 4
  • 5.
    Static Blocks • Staticblock is a block of code prefixed by ‘static’ keyword • Gets executed only once • Used to initialize static variables class A { static int x; static { static block x = 10; initializes } x to 10 public static void main (String args[]) { System.out.println (“ x value is : ”+ x); } } Output : x value is : 10 http://improvejava.blogspot.in 5
  • 6.
    Example Program :Static Block class A { static int x = 10; static int y; static void call( int p) { System .out. println(“ x value is :”+x); System .out .println(“ y value is :”+y); System .out .println(“ p value is :”+p); } static { System.out.println(“ static block initialized”); y=a*2; } public static void main (String args[]) { call(30); Output } x value is : 10 } y value is : 20 p value is : 30 http://improvejava.blogspot.in 6
  • 7.
    Final Variables • Avariable can be declared as final • Doing so prevents its contents from being modified • This means that you must initialize a final variable when it is declared • Permits us to create typed constants • In usage, final is similar to const in C / C++ http://improvejava.blogspot.in 7
  • 8.
    Example final variables Syntax final type constName = value; Example final int FILE_NEW = 1; final float PI = 3.141519; http://improvejava.blogspot.in 8
  • 9.
    Final Variables • Subsequentparts of the program may use the above final variables FILE_NEW and PI • It is common coding convention to choose all uppercase identifiers for final variables • Variables declared as final do not occupy memory on a per- instance basis • Thus, a final variable is essentially a constant http://improvejava.blogspot.in 9
  • 10.
    Example program :final variables Correct program Wrong program class A { class B { final int X = 10; final int X = 10; public static void main (String args[]) public static void main(String args[]) { { X = 20; System.out.println(“ X is “+X); System .out. println(“ X is “+X); } } } } final variable should not change http://improvejava.blogspot.in 10
  • 11.
    Other Uses Offinal • The keyword final can also be applied to methods, • Its meaning is substantially different than when it is applied to variables http://improvejava.blogspot.in 11
  • 12.
    Summary • Static blocks •Final variables http://improvejava.blogspot.in 12
  • 13.
    Assignment 1. Write aclass that implements static blocks 2. Write a class that uses final variables http://improvejava.blogspot.in
  • 14.
    Quiz 1. Static blocksare executed a. Java program is compiled b. Only one time c. Several times d. None http://improvejava.blogspot.in 14
  • 15.
    Quiz contd.. 2. Final variable is a. variable b. instance variable c. constant d. static variable http://improvejava.blogspot.in
  • 16.
    Frequently Asked Questions 1.Explain the use of static blocks 2. Explain the use of final variables http://improvejava.blogspot.in 16