SlideShare ist ein Scribd-Unternehmen logo
1 von 16
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

Weitere ähnliche Inhalte

Was ist angesagt?

OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesRatnaJava
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentSuresh Mohta
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and notSalvatore Fazio
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Raffi Khatchadourian
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMURaffi Khatchadourian
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.netsuraj pandey
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...Raffi Khatchadourian
 
Template Method Pattern
Template Method PatternTemplate Method Pattern
Template Method Patternmonisiqbal
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11Terry Yoast
 

Was ist angesagt? (20)

OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and not
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
L06 process design
L06 process designL06 process design
L06 process design
 
Java interface
Java interfaceJava interface
Java interface
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Php day2010
Php day2010Php day2010
Php day2010
 
Template Method Pattern
Template Method PatternTemplate Method Pattern
Template Method Pattern
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
 
14 interface
14  interface14  interface
14 interface
 

Andere mochten auch

Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 
OOP and java by a introduction sandesh sharma
OOP and java by a introduction sandesh sharmaOOP and java by a introduction sandesh sharma
OOP and java by a introduction sandesh sharmaSandesh Sharma
 
Access specifier in java
Access specifier in javaAccess specifier in java
Access specifier in javaKaml Sah
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)indiangarg
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptionsİbrahim Kürce
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)Muhammad Hammad Waseem
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in javaTech_MX
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design ExamplesTareq Hasan
 

Andere mochten auch (20)

Final keyword
Final keywordFinal keyword
Final keyword
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
OOP and java by a introduction sandesh sharma
OOP and java by a introduction sandesh sharmaOOP and java by a introduction sandesh sharma
OOP and java by a introduction sandesh sharma
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Access specifier in java
Access specifier in javaAccess specifier in java
Access specifier in java
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Java(Access Modifiers)
Java(Access Modifiers)Java(Access Modifiers)
Java(Access Modifiers)
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptions
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Drug degradation impurity in excipients
Drug degradation impurity in excipients Drug degradation impurity in excipients
Drug degradation impurity in excipients
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
 
1z0-808-certification-questions-sample
1z0-808-certification-questions-sample1z0-808-certification-questions-sample
1z0-808-certification-questions-sample
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
 

Ähnlich wie Static blocks, final variables .19

Constructors.16
Constructors.16Constructors.16
Constructors.16myrajendra
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptxSRKCREATIONS
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptxsyedabbas594247
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++PRINCE KUMAR
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdfKUNALHARCHANDANI1
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of featuresvidyamittal
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15myrajendra
 

Ähnlich wie Static blocks, final variables .19 (20)

Static.18
Static.18Static.18
Static.18
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Exploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systemsExploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systems
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
 
OOPC_Unit-I.pdf
OOPC_Unit-I.pdfOOPC_Unit-I.pdf
OOPC_Unit-I.pdf
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
Advanced Java Testing
Advanced Java TestingAdvanced Java Testing
Advanced Java Testing
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
 
Java
JavaJava
Java
 

Mehr von myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Static blocks, final variables .19

  • 1. Working of static blocks, final variables http://improvejava.blogspot.in 1
  • 2. Objective • On completion of this period, you would be able to know • Static blocks • Final variables http://improvejava.blogspot.in
  • 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. 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 • 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
  • 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 • 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. 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 • 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. 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 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. Summary • Static blocks • Final variables http://improvejava.blogspot.in 12
  • 13. Assignment 1. Write a class that implements static blocks 2. Write a class that uses final variables http://improvejava.blogspot.in
  • 14. 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
  • 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