Your SlideShare is downloading. ×
0
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Object Oriented Concept Static vs. Non Static

678

Published on

Static is not the true intend of Object Oriented Design and Concept. …

Static is not the true intend of Object Oriented Design and Concept.
For instance, we turn a LAMP "off" it does not suppose to turn the LAMPS of the entire world goes "off".

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
678
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Static vs. Non Static https://www.facebook.com/Oxus20 oxus20@gmail.com Object Oriented Concept Abdul Rahman Sherzad
  • 2. Static vs. Non-static » Static ˃ class variable ˃ class method » Non-static ˃ instance variable ˃ instance method » The absence of the keyword static before non-local variables and methods means dynamic / non-static (one per object / instance) » Static ones are associated with class, not object. Can be called using class name directly 2 https://www.facebook.com/Oxus20
  • 3. Static Variables » Static means "refer to to the class in general", not to an individual object » A variable may be declared (outside of a method) with the static keyword: ˃ e.g. static int numberOfEmployees; ˃ There is one variable numberOfEmployees for the class not one per object!!! » A static variable is shared by all instances (if any). ˃ All instances may be able read / write it 3 https://www.facebook.com/Oxus20
  • 4. Static Methods » A method may be declared with the static keyword » Static methods live at class level, not at object level » Static methods may access static variables and methods, but not dynamic ones! public class Employee { private static int numberOfEmployees; public static int getNumberOfEmployee() { return numberOfEmployees; } } 4 https://www.facebook.com/Oxus20
  • 5. Logical Example » Static is not the true intend of Object Oriented Design and Concept. » Based on following scenario: » LAMP is an object. OFF / ON is its state and characteristic; the state of one object is independent of another. » For instance, we turn a LAMP "off" it does not suppose to turn the LAMPS of the entire world goes "off". 5 https://www.facebook.com/Oxus20
  • 6. Example Demonstrates Bad Use of Static public class BankAccount { private String id; private String name; // 'static' only for demonstration purpose, in real example must not static! private static double balance; public BankAccount(String i, String n, double b) { id = i; name = n; balance = b; } public String getId() { return id; } public String getName() { return name; } https://www.facebook.com/Oxus20 6
  • 7. Example Demonstrates Bad Use of Static (contd.) public double getBalance() { return balance; } public boolean deposit(double amount) { if (amount > 0) { balance += amount; return true; } return false; } public boolean withdraw(double amount) { // at least 100 must be kept in Balance if (balance > amount + 100) { balance -= amount; return true; } return false; } } 7 https://www.facebook.com/Oxus20
  • 8. Use BankAccount Class public class BankApplication { public static void main(String[] args) { BankAccount absherzad = new BankAccount("20120", "Abdul Rahman Sherzad", 500); absherzad.deposit(1500); // print the balance for object 'absherzad' System.out.println(absherzad.getBalance()); // 2000 BankAccount oxus20 = new BankAccount("20800", "OXUS 20", 400); /* * print the balance for object 'absherzad' * why output 300? because balance variable defined 'static' * 'static' variable is not dependent on object * when account for 'oxus20' created with initial balance of 400 * Now all the objects of class BankAccount has balance of 400 */ System.out.println(absherzad.getBalance()); // 400 } } 8 https://www.facebook.com/Oxus20
  • 9. BankAccount Example Conclusion » for demonstration purpose the balance variable for the class BankAccount was defined 'static' » BankApplication class demonstrates how the last object initial balance overrides all the balance for entire objects of class BankAccount. » That is why, it is to be said that "Static is not the true intend of Object Oriented Concept" 9 https://www.facebook.com/Oxus20
  • 10. When Use Static » A variable should be static if: ˃ It logically describes the class as a whole ˃ There should be only one copy of it » A method should be static if: ˃ It does not use or affect the object that receives the message (it uses only its parameters) » When a policy is applied for the whole class. For example, when you ask your students in the class to be quite, stop writing, etc. you ask the entire class rather than a particular student. 10 https://www.facebook.com/Oxus20
  • 11. Static Rules » static variables and methods belong to the class in general, not to individual objects » The absence of the keyword static before non-local variables and methods means dynamic (one per object / instance) » A dynamic method can access all dynamic and static variables and methods in the same class » A static method can not access a dynamic variable (How could it choose or which one?) » A static method can not call a dynamic method (because it might access an instance variable 11 https://www.facebook.com/Oxus20
  • 12. END 12 https://www.facebook.com/Oxus20

×