KEMBAR78
Object Oriented Concept Static vs. Non Static | PDF
Static
vs.
Non Static

https://www.facebook.com/Oxus20

oxus20@gmail.com

Object
Oriented
Concept
Abdul Rahman Sherzad
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
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
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
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
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
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
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
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
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
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
END

12

https://www.facebook.com/Oxus20

Object Oriented Concept Static vs. Non Static

  • 1.
  • 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 » Staticmeans "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 » Amethod 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 » Staticis 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 BadUse 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 BadUse 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 publicclass 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 » staticvariables 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.