SlideShare une entreprise Scribd logo
1  sur  19
Working of static variables, methods




                       http://improvejava.blogspot.in
                                                        1
Objective


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


          • Static member

          • Static variables

          • Static methods




                               http://improvejava.blogspot.in
Recap

In the previous lesson, we have discussed
   • ‘this’ pointer
   • Usage of ‘this’ pointer
   • Related programs




               http://improvejava.blogspot.in
Static Member


• Normally a class member must be accessed only in
  conjunction with an object of its class
• However, it is possible to create a member that can be used
  by itself, without reference to a specific instance
• Such a member is called static member




                     http://improvejava.blogspot.in
                                                                4
Static Member

• To create such a member, precede its declaration with the
  keyword static

• 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()

• main() is declared as static because it must be called before any
  object exist.
                        http://improvejava.blogspot.in
                                                                       5
Static Variable
• You can declare both methods and variables to be static

• Instance variables declared as static are, essentially,
  global variables

• Such variables are called as class variables

• For a class variable only one copy of memory is allotted
  regardless of any number of objects created




                     http://improvejava.blogspot.in
                                                             6
Example Program : Non-Static Variable
class A {
      int x;                                       non static variable
      public static void main (String args[]) {
          A obj_a = new A();
          obj_a.x = 10;
          System.out.println (“ value of x in obj_a ”+ obj_a.x);
          A obj_b = new A();
          System.out.println(“value of x in obj_b ”+ obj_b.x);
      }
}
Output : x value in object pointed by obj_a 10
         x value in object pointed by obj_b “garbage value”


                            http://improvejava.blogspot.in
                                                                         7
Example Program :Non-Static Variable
Memory allocation for the objects in the above program


     • A obj_a = new A();                       • A obj_b = new A();
     • obj_a.x = 10;



            x :10                                        x :gb


            obj_a   for non-static variables             obj_b
                    memory is allocated for
                     each object individually

                        http://improvejava.blogspot.in
                                                                       8
Example program :Static Variable

class A {
      static int x;
      public static void main (String args[]) {
         A obj_a = new A();
         obj_a.x = 10;
         System.out.println(“value of x in obj_a ”+ obj_a.x);
         A obj_b = new A();
         System.out.println(“value of x in obj_b ”+ obj_b.x);
      }
}



                     http://improvejava.blogspot.in
Example Program :Non-Static Variable
Memory allocation for the objects in the above program


     • A obj_a = new A();                      • A obj_b = new A();
     • obj_a.x = 10;


                               x :10


                    For static variables memory
            obj_a   is allocated for each object
                                                        obj_b
                    in common place
                    not inside object
                       http://improvejava.blogspot.in
                                                                      10
Static Methods


• Methods declared as static have several restrictions

   • They can only call other static methods

   • They must only access static data

   • They cannot refer to this or super in any way




                          http://improvejava.blogspot.in
                                                           11
Example Program : Static Methods
class A {
      static int x=10;
      static void call (int y) {
              System.out.println(“ x value is : ”+ x);
              System.out.println(“ y value is : ”+ y);
      }
      public static void main (String args[]) {
         call(20);
      }
}
Output : x value is : 10
           y value is : 2                      call() is called without
                                                        creating object because
                                                        call() is static method


                       http://improvejava.blogspot.in
Calling Static Methods

• Normally to call a method, its object is need
• To call total_mks() of Student class we need to create student
  object and then call total_mks on thaat objecxt
                • Student s = new Student();
                • s.total_mks();
• To call static method, there is no need to create an object of the
  class
• To call it use the class name directly
            • ClassName.methodName();



                          http://improvejava.blogspot.in
                                                                       13
Example program : Calling Static Methods
class A {
  static int a = 10;
  static int b = 20;
  static void callme() {
          System.out.println(“ a: ”+a);
  }
}
class B {
     public static void main() {                          calling static method
          A.callme();                                     without using object
          System.out.println(“ b: ”+A.b);                 reference
     }
}
  Output:
                                                  static variable is
     a: 10
                                                  Accessed without
     b: 20
                                                  object reference
                       http://improvejava.blogspot.in
                                                                                  14
Summary


• Static member
• Static variables
• Static methods




                     http://improvejava.blogspot.in
                                                      15
Quiz
1. Static variable memory is created
   a. Like instance variable
   b. Separate memory for each object
   c. Common memory for all object
   d. None




                   http://improvejava.blogspot.in
                                                    16
Quiz                       contd..
2. Static variables or methods are accessed:
   a. using object
   b. using reference
   c. using class name from outside class.
   d. none




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


1. Explain the use of static variables?

2. Explain the concept of static methods?




                    http://improvejava.blogspot.in
                                                     18
Assignment

1. Write an Employee class assuming appropriate data
   and method members
   Create objects for the above class, and display
   how many number of objects are created by using
   static member concept




                   http://improvejava.blogspot.in

Contenu connexe

Tendances

PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboardsDenis Ristic
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3PawanMM
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Ramamohan Chokkam
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part IIIHari Christian
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan vo
 

Tendances (20)

Objective c
Objective cObjective c
Objective c
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
 
Ajaxworld
AjaxworldAjaxworld
Ajaxworld
 
Java Day-3
Java Day-3Java Day-3
Java Day-3
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Dot Net csharp Language
Dot Net csharp LanguageDot Net csharp Language
Dot Net csharp Language
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
Oop
OopOop
Oop
 
Prototype
PrototypePrototype
Prototype
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 

Similaire à Static.18

Static blocks, final variables .19
Static blocks, final variables .19Static blocks, final variables .19
Static blocks, final variables .19myrajendra
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptxsyedabbas594247
 
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
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxDrYogeshDeshmukh1
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Constructors.16
Constructors.16Constructors.16
Constructors.16myrajendra
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3Mahmoud Alfarra
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
Week10 packages using objects in objects
Week10 packages using objects in objectsWeek10 packages using objects in objects
Week10 packages using objects in objectskjkleindorfer
 
Effective java
Effective javaEffective java
Effective javaEmprovise
 

Similaire à Static.18 (20)

Static blocks, final variables .19
Static blocks, final variables .19Static blocks, final variables .19
Static blocks, final variables .19
 
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
 
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
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
Constructor
ConstructorConstructor
Constructor
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Object Oriented JavaScript - II
Object Oriented JavaScript - IIObject Oriented JavaScript - II
Object Oriented JavaScript - II
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Week10 packages using objects in objects
Week10 packages using objects in objectsWeek10 packages using objects in objects
Week10 packages using objects in objects
 
Effective java
Effective javaEffective java
Effective java
 

Plus de 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.18

  • 1. Working of static variables, methods http://improvejava.blogspot.in 1
  • 2. Objective On completion of this period, you would be able to know : • Static member • Static variables • Static methods http://improvejava.blogspot.in
  • 3. Recap In the previous lesson, we have discussed • ‘this’ pointer • Usage of ‘this’ pointer • Related programs http://improvejava.blogspot.in
  • 4. Static Member • Normally a class member must be accessed only in conjunction with an object of its class • However, it is possible to create a member that can be used by itself, without reference to a specific instance • Such a member is called static member http://improvejava.blogspot.in 4
  • 5. Static Member • To create such a member, precede its declaration with the keyword static • 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() • main() is declared as static because it must be called before any object exist. http://improvejava.blogspot.in 5
  • 6. Static Variable • You can declare both methods and variables to be static • Instance variables declared as static are, essentially, global variables • Such variables are called as class variables • For a class variable only one copy of memory is allotted regardless of any number of objects created http://improvejava.blogspot.in 6
  • 7. Example Program : Non-Static Variable class A { int x; non static variable public static void main (String args[]) { A obj_a = new A(); obj_a.x = 10; System.out.println (“ value of x in obj_a ”+ obj_a.x); A obj_b = new A(); System.out.println(“value of x in obj_b ”+ obj_b.x); } } Output : x value in object pointed by obj_a 10 x value in object pointed by obj_b “garbage value” http://improvejava.blogspot.in 7
  • 8. Example Program :Non-Static Variable Memory allocation for the objects in the above program • A obj_a = new A(); • A obj_b = new A(); • obj_a.x = 10; x :10 x :gb obj_a for non-static variables obj_b memory is allocated for each object individually http://improvejava.blogspot.in 8
  • 9. Example program :Static Variable class A { static int x; public static void main (String args[]) { A obj_a = new A(); obj_a.x = 10; System.out.println(“value of x in obj_a ”+ obj_a.x); A obj_b = new A(); System.out.println(“value of x in obj_b ”+ obj_b.x); } } http://improvejava.blogspot.in
  • 10. Example Program :Non-Static Variable Memory allocation for the objects in the above program • A obj_a = new A(); • A obj_b = new A(); • obj_a.x = 10; x :10 For static variables memory obj_a is allocated for each object obj_b in common place not inside object http://improvejava.blogspot.in 10
  • 11. Static Methods • Methods declared as static have several restrictions • They can only call other static methods • They must only access static data • They cannot refer to this or super in any way http://improvejava.blogspot.in 11
  • 12. Example Program : Static Methods class A { static int x=10; static void call (int y) { System.out.println(“ x value is : ”+ x); System.out.println(“ y value is : ”+ y); } public static void main (String args[]) { call(20); } } Output : x value is : 10 y value is : 2 call() is called without creating object because call() is static method http://improvejava.blogspot.in
  • 13. Calling Static Methods • Normally to call a method, its object is need • To call total_mks() of Student class we need to create student object and then call total_mks on thaat objecxt • Student s = new Student(); • s.total_mks(); • To call static method, there is no need to create an object of the class • To call it use the class name directly • ClassName.methodName(); http://improvejava.blogspot.in 13
  • 14. Example program : Calling Static Methods class A { static int a = 10; static int b = 20; static void callme() { System.out.println(“ a: ”+a); } } class B { public static void main() { calling static method A.callme(); without using object System.out.println(“ b: ”+A.b); reference } } Output: static variable is a: 10 Accessed without b: 20 object reference http://improvejava.blogspot.in 14
  • 15. Summary • Static member • Static variables • Static methods http://improvejava.blogspot.in 15
  • 16. Quiz 1. Static variable memory is created a. Like instance variable b. Separate memory for each object c. Common memory for all object d. None http://improvejava.blogspot.in 16
  • 17. Quiz contd.. 2. Static variables or methods are accessed: a. using object b. using reference c. using class name from outside class. d. none http://improvejava.blogspot.in
  • 18. Frequently Asked Questions 1. Explain the use of static variables? 2. Explain the concept of static methods? http://improvejava.blogspot.in 18
  • 19. Assignment 1. Write an Employee class assuming appropriate data and method members Create objects for the above class, and display how many number of objects are created by using static member concept http://improvejava.blogspot.in