static blocks, final variables .19
TRANSCRIPT
1
Working of static blocks, final variables
http://improvejava.blogspot.in
http://improvejava.blogspot.in
Objective
• On completion of this period, you would be able to know
• Static blocks
• Final variables
3
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
4
• 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()
Recap contd..
http://improvejava.blogspot.in
5
Static Blocks• Static block is a block of code prefixed by ‘static’ keyword• Gets executed only once• Used to initialize static variables
static block initializes
x to 10
class A { static int x; static { x = 10; } public static void main (String args[]) { System.out.println (“ x value is : ”+ x); }}Output : x value is : 10
http://improvejava.blogspot.in
6
Example Program : Static Block
Output x value is : 10 y value is : 20 p value is : 30
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); }}
http://improvejava.blogspot.in
7
• 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++
Final Variables
http://improvejava.blogspot.in
Example final variables
8
Syntax final type constName = value;
Example final int FILE_NEW = 1;final float PI = 3.141519;
http://improvejava.blogspot.in
Final Variables
9
• 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
Example program : final variables
10
final variable should not change
class A { final int X = 10; public static void main (String args[]) { System.out.println(“ X is “+X); }}
class B { final int X = 10; public static void main(String args[]) { X = 20; System .out. println(“ X is “+X); }}
Correct program Wrong program
http://improvejava.blogspot.in
Other Uses Of final
11
• 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
• Static blocks • Final variables
12
Summary
http://improvejava.blogspot.in
1. Write a class that implements static blocks
2. Write a class that uses final variables
Assignment
http://improvejava.blogspot.in
1. Static blocks are executed a. Java program is compiledb. Only one timec. Several timesd. None
14
Quiz
http://improvejava.blogspot.in
Quiz contd..
2. Final variable is a. variableb. instance variablec. constantd. static variable
http://improvejava.blogspot.in
1. Explain the use of static blocks
2. Explain the use of final variables
16
Frequently Asked Questions
http://improvejava.blogspot.in