Static blocks, final variables .19

729 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
729
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Static blocks, final variables .19

  1. 1. Working of static blocks, final variables http://improvejava.blogspot.in 1
  2. 2. Objective• On completion of this period, you would be able to know • Static blocks • Final variables http://improvejava.blogspot.in
  3. 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 3
  4. 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. 5. Static Blocks• Static block is a block of code prefixed by ‘static’ keyword• Gets executed only once• Used to initialize static variablesclass 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. 6. Example Program : Static Blockclass 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. 7. 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
  8. 8. Example final variablesSyntax final type constName = value;Example final int FILE_NEW = 1; final float PI = 3.141519; http://improvejava.blogspot.in 8
  9. 9. 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
  10. 10. Example program : final variables Correct program Wrong programclass 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. 11. 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
  12. 12. Summary• Static blocks• Final variables http://improvejava.blogspot.in 12
  13. 13. Assignment1. Write a class that implements static blocks2. Write a class that uses final variables http://improvejava.blogspot.in
  14. 14. Quiz1. Static blocks are executed a. Java program is compiled b. Only one time c. Several times d. None http://improvejava.blogspot.in 14
  15. 15. Quiz contd..2. Final variable is a. variable b. instance variable c. constant d. static variable http://improvejava.blogspot.in
  16. 16. Frequently Asked Questions1. Explain the use of static blocks2. Explain the use of final variables http://improvejava.blogspot.in 16

×