SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

Object Modelling in Software Engineering
Object Modelling in Software EngineeringObject Modelling in Software Engineering
Object Modelling in Software Engineering
guest7fe55d5e
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12
koolkampus
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
Komal Singh
 

Andere mochten auch (13)

Hands-on Guide to Object Identification
Hands-on Guide to Object IdentificationHands-on Guide to Object Identification
Hands-on Guide to Object Identification
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Lecture 2 introduction to Software Engineering 1
Lecture 2   introduction to Software Engineering 1Lecture 2   introduction to Software Engineering 1
Lecture 2 introduction to Software Engineering 1
 
Software Engineering 2 lecture slide
Software Engineering 2 lecture slideSoftware Engineering 2 lecture slide
Software Engineering 2 lecture slide
 
1-Introduction to Software Engineering (Object Oriented Software Engineering ...
1-Introduction to Software Engineering (Object Oriented Software Engineering ...1-Introduction to Software Engineering (Object Oriented Software Engineering ...
1-Introduction to Software Engineering (Object Oriented Software Engineering ...
 
Object Modelling in Software Engineering
Object Modelling in Software EngineeringObject Modelling in Software Engineering
Object Modelling in Software Engineering
 
Software Engineering unit 2
Software Engineering unit 2Software Engineering unit 2
Software Engineering unit 2
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Object Oriented Software Engineering
Object Oriented Software EngineeringObject Oriented Software Engineering
Object Oriented Software Engineering
 
Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12
 
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
Lecture 2 Software Engineering and Design Object Oriented Programming, Design...
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
 

Ähnlich wie Object Oriented Concept Static vs. Non Static

Java Methods
Java MethodsJava Methods
Java Methods
OXUS 20
 

Ähnlich wie Object Oriented Concept Static vs. Non Static (19)

Java basics
Java basicsJava basics
Java basics
 
Everything about Object Oriented Programming
Everything about Object Oriented ProgrammingEverything about Object Oriented Programming
Everything about Object Oriented Programming
 
Object Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesObject Oriented Programming with Real World Examples
Object Oriented Programming with Real World Examples
 
Classes2
Classes2Classes2
Classes2
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Chap11
Chap11Chap11
Chap11
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
 
Object oriented thinking
Object oriented thinkingObject oriented thinking
Object oriented thinking
 

Mehr von Abdul Rahman Sherzad

Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and Awareness
Abdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 

Mehr von Abdul Rahman Sherzad (20)

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

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

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