Object Oriented Concept Static vs. Non Static

50 %
50 %
Information about Object Oriented Concept Static vs. Non Static
Education

Published on January 19, 2014

Author: absherzad

Source: slideshare.net

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

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

Add a comment

Related presentations

Related pages

Language Basics / Object-Oriented Programming / Static ...

Language Basics / Object-Oriented Programming / Static Members of a Class ... Non-static data members are created for each class object variable.
Read more

The difference between regular (non-static) and static methods

Java is a Object Oriented Programming ... OOPs Concept | Updated: January 11, 2014. ... Static methods can’t use non-static ...
Read more

object oriented - Can't I just use all static methods ...

... pure object oriented and have a kind of idiomatic view ... Calling a non-static ... (and the general approach of "let's use statics because they ...
Read more

object oriented - Why have private static methods ...

... (some code that does not depend on non-static ... Browse other questions tagged object-oriented class methods static ... "I did my best" vs "I did ...
Read more

oop - When should I use static methods in a class and what ...

I have concept of static variables but what are the benefits of static methods in a class. ... The concept of a static variable in Object Oriented terms is ...
Read more

Static Classes and Static Class Members (C# Programming Guide)

Static Classes and Static ... const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object ...
Read more

What is dynamic and static? - Definition from WhatIs.com

This creates more flexible programs and can simplify prototyping and some object-oriented coding. In a static ... dynamic and static ... no matter where it ...
Read more

PHP: Static Keyword - Manual - PHP: Hypertext Preprocessor

This page describes the use of the static keyword to define static ... and non static members is only that a non ... of object oriented ...
Read more

Nested Classes (The Java™ Tutorials > Learning the Java ...

Non-static nested classes (inner classes) have access to other members of the enclosing class, ... to create an object for the static nested class, ...
Read more