Object Oriented Concept Static vs. Non Static

Post on 06-May-2015

659 views 1 download

description

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".

Transcript of Object Oriented Concept Static vs. Non Static

Object Oriented Concept

Static

vs.

Non Static

Abdul Rahman Sherzad

https://www.facebook.com/Oxus20

oxus20@gmail.com

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

https://www.facebook.com/Oxus20

2

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;

} 6

https://www.facebook.com/Oxus20

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