SlideShare a Scribd company logo
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

More Related Content

Viewers also liked (13)

PDF
Hands-on Guide to Object Identification
Dharmesh Vaya
 
PPT
Object Oriented Concepts and Principles
deonpmeyer
 
PPT
Lecture 2 introduction to Software Engineering 1
IIUI
 
PPT
Software Engineering 2 lecture slide
Adil Mehmoood
 
PDF
1-Introduction to Software Engineering (Object Oriented Software Engineering ...
Hafiz Ammar Siddiqui
 
PPTX
Object Modelling in Software Engineering
guest7fe55d5e
 
PPTX
Software Engineering unit 2
Abhimanyu Mishra
 
PPT
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
PPTX
Object Oriented Software Engineering
Ali Haider
 
PPT
Object Oriented Design in Software Engineering SE12
koolkampus
 
PDF
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
op205
 
PPTX
Object identification and its management
Vinay Kumar Pulabaigari
 
PDF
Object oriented software engineering concepts
Komal Singh
 
Hands-on Guide to Object Identification
Dharmesh Vaya
 
Object Oriented Concepts and Principles
deonpmeyer
 
Lecture 2 introduction to Software Engineering 1
IIUI
 
Software Engineering 2 lecture slide
Adil Mehmoood
 
1-Introduction to Software Engineering (Object Oriented Software Engineering ...
Hafiz Ammar Siddiqui
 
Object Modelling in Software Engineering
guest7fe55d5e
 
Software Engineering unit 2
Abhimanyu Mishra
 
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Object Oriented Software Engineering
Ali Haider
 
Object Oriented Design in Software Engineering SE12
koolkampus
 
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
op205
 
Object identification and its management
Vinay Kumar Pulabaigari
 
Object oriented software engineering concepts
Komal Singh
 

Similar to Object Oriented Concept Static vs. Non Static (20)

PPTX
Lecture_4_Static_variables_and_Methods.pptx
IkaDeviPerwitasari1
 
PPTX
Lecture 6.pptx
AshutoshTrivedi30
 
PPTX
Augumented course for vtu topic non static method in java
ESAIKRISHNA
 
PPT
Static.18
myrajendra
 
PPTX
Week10 packages using objects in objects
kjkleindorfer
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Classes-and-Object.pptx
VishwanathanS5
 
PPTX
class as the basis.pptx
Epsiba1
 
PPTX
6. static keyword
Indu Sharma Bhardwaj
 
DOCX
Java sessionnotes
Lakshmi Sarvani Videla
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Java static keyword
Ahmed Shawky El-faky
 
PDF
Understanding the Static Keyword in C#: A Beginner’s Guide
StudySection
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
This and Static Keyword
Dhrumil Panchal
 
PPTX
11 static
dhrubo kayal
 
PPTX
3.Classes&Objects.pptx
PRABHUSOLOMON1
 
DOCX
Object oriented programming tutorial
Ghulam Abbas Khan
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Think with java
Arifur Rahman
 
Lecture_4_Static_variables_and_Methods.pptx
IkaDeviPerwitasari1
 
Lecture 6.pptx
AshutoshTrivedi30
 
Augumented course for vtu topic non static method in java
ESAIKRISHNA
 
Static.18
myrajendra
 
Week10 packages using objects in objects
kjkleindorfer
 
Static keyword ppt
Vinod Kumar
 
Classes-and-Object.pptx
VishwanathanS5
 
class as the basis.pptx
Epsiba1
 
6. static keyword
Indu Sharma Bhardwaj
 
Java sessionnotes
Lakshmi Sarvani Videla
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Java static keyword
Ahmed Shawky El-faky
 
Understanding the Static Keyword in C#: A Beginner’s Guide
StudySection
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
This and Static Keyword
Dhrumil Panchal
 
11 static
dhrubo kayal
 
3.Classes&Objects.pptx
PRABHUSOLOMON1
 
Object oriented programming tutorial
Ghulam Abbas Khan
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Think with java
Arifur Rahman
 
Ad

More from Abdul Rahman Sherzad (20)

PDF
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PDF
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
PDF
Iterations and Recursions
Abdul Rahman Sherzad
 
PDF
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
PDF
PHP Variable variables Examples
Abdul Rahman Sherzad
 
PDF
Cross Join Example and Applications
Abdul Rahman Sherzad
 
PDF
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
PDF
Web Application Security and Awareness
Abdul Rahman Sherzad
 
PDF
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
PDF
Mobile Score Notification System
Abdul Rahman Sherzad
 
PDF
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
PDF
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
PDF
Java Applet and Graphics
Abdul Rahman Sherzad
 
PDF
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
PDF
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
PDF
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
PDF
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
Iterations and Recursions
Abdul Rahman Sherzad
 
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
PHP Variable variables Examples
Abdul Rahman Sherzad
 
Cross Join Example and Applications
Abdul Rahman Sherzad
 
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
Web Application Security and Awareness
Abdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
Mobile Score Notification System
Abdul Rahman Sherzad
 
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
Java Applet and Graphics
Abdul Rahman Sherzad
 
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
Ad

Recently uploaded (20)

PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 

Object Oriented Concept Static vs. Non Static

  • 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