Static.18

263 views
196 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
263
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Static.18

  1. 1. Working of static variables, methods http://improvejava.blogspot.in 1
  2. 2. ObjectiveOn completion of this period, you would be able to know : • Static member • Static variables • Static methods http://improvejava.blogspot.in
  3. 3. RecapIn the previous lesson, we have discussed • ‘this’ pointer • Usage of ‘this’ pointer • Related programs http://improvejava.blogspot.in
  4. 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. 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. 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. 7. Example Program : Non-Static Variableclass 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. 8. Example Program :Non-Static VariableMemory 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. 9. Example program :Static Variableclass 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. 10. Example Program :Non-Static VariableMemory 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. 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. 12. Example Program : Static Methodsclass 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. 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. 14. Example program : Calling Static Methodsclass 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. 15. Summary• Static member• Static variables• Static methods http://improvejava.blogspot.in 15
  16. 16. Quiz1. 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. 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. 18. Frequently Asked Questions1. Explain the use of static variables?2. Explain the concept of static methods? http://improvejava.blogspot.in 18
  19. 19. Assignment1. 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

×