SlideShare a Scribd company logo
The story of Static keywords’ behavior in Java
Static is kind a opposite to Object Creation. Say you have a class person as
below:
class Person{
private int age;
// suppose it has getters and setters
}
Object of Person can be created in the main class like this:
Person p1 = new Person();
p1 is known as the object reference. The term hints on its bahaviour. It is a
pointer (a variable that can hold memory addresses) to a memory location where
object is situated. The memory where objects live is known as Heap. So object
reference point to a memory address on the heap which houses an int age field.
So in a way, you can say int age is the p1 object’s private data right?
Make sure you understand this.
Now lets suppose, it has another field say int count, forget about its purpose for
now!
So the new class would become:
class Person{
private int age;
static int count;
// suppose it has getters and setters for both
}
Notice it is static! What does that imply. As described in the lecture. Static
keyword changes the behavior of the field in focus i.e. int amount. It is no more
part of the object’s private data. It also implies it is not on the heap right?
As said during the lectures, it is actually associated with class. If you create
several object from the class Person say p1, p2, p3. All of these may have their
private data but do have the class Person in common. Static paves a way to
share data among these objects by associating it with the class. So static
features are associated with the class not with objects, memorize it now!
It can be accessed in the main method of the Main class like this. I am not writing
the whole main method’s signature for brevity.
//inside main
Person p1 = new Person();
p1.count; // what the hell!!!
you said its not part of the object’s private data. Well, its still not part of the
object’s private data and is not on the heap but java provides you with a way to
still access it through the object reference.
There is another way to access this, which is a better and more meaningful way
i.e. access it through the class name. So we access class Person’s amount field
like this:
// inside Main class’s main method
// i.e. outside the person class.
Person.amount;
As said earlier, all the object references p1, p2, p3 share a common class. The
quantity of a class is one. It means you create multiple objects from one class. It
also means class is written once for one or more objects to be created from it.
So if you create an amount field, it will also be one since it is associated with
class which is single (to be exact).
Suppose you have a Person as written in the second example (i.e. OO and static
fields) and you create three objects from it i.e. p1, p2, p3. How many
occurrences of age variable would be? And what is the count of the amount field
created in this scenario.
Ans: 3 objects so 3 occurrences of age fields inside each object.
and only occurrence of static field amount.
In addition to fields, you can also have static method. Its access is again similar
to the static field i.e. through class and object reference.
You can also have a static block as shown below:
class Person{
static{
sysout(“do whatever you like here”);
}
private int age;
static int amount;
Person(){}
// suppose it has getters and setters for both
}
Static block runs even before the constructor, normally used to load a native
routine in java. Or anything you wish to do once (as the class loads in JVM) even
before the constructor runs.
Another importance rule to mention here is static can only access another static
feature of the class e.g.
public class App {
int a;
public static void main( String[] args ){
System.out.println("trying to print a:"+a);
}
}
Error arises as below:
Cannot make a static reference to the non-static field a
Solution 1 to the above problem is through object creation as a is an OO field.
public class App {
int a;
public static void main( String[] args ){
App app = new App();
System.out.println("Access a like this:"+app.a);
}
}
Solution 2, make the field static as well
public class App {
static int a;
public static void main( String[] args ){
System.out.println("Access a by making it static inside
main method "
+ "which is also static:"+a);
}
}

More Related Content

Similar to static-story.pdf

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
Ahmad sohail Kakar
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
Infocampus Logics Pvt.Ltd.
 
Use Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.pptUse Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.ppt
manishchoudhary91861
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
Chapter 7 java
Chapter 7 javaChapter 7 java
Chapter 7 java
Ahmad sohail Kakar
 
Oop.concepts
Oop.conceptsOop.concepts
Oop.concepts
tahir266
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
sandy14234
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
sinhacp
 
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its MetaprogrammingMetaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Inexture Solutions
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
raahulwasule
 
OOPS
OOPSOOPS
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
Shambhavi Vats
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.ppt
Ravi Kumar
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
Usman Mehmood
 
My c++
My c++My c++
My c++
snathick
 
Object in python tells about object oriented programming in python
Object in python tells about object oriented programming in pythonObject in python tells about object oriented programming in python
Object in python tells about object oriented programming in python
ReshmiShaw2
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 

Similar to static-story.pdf (20)

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Basic object oriented concepts (1)
Basic object oriented concepts (1)Basic object oriented concepts (1)
Basic object oriented concepts (1)
 
Use Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.pptUse Classes with Object-Oriented Programming in C++.ppt
Use Classes with Object-Oriented Programming in C++.ppt
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Chapter 7 java
Chapter 7 javaChapter 7 java
Chapter 7 java
 
Oop.concepts
Oop.conceptsOop.concepts
Oop.concepts
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
 
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its MetaprogrammingMetaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
 
OOPS
OOPSOOPS
OOPS
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
 
38-object-concepts.ppt
38-object-concepts.ppt38-object-concepts.ppt
38-object-concepts.ppt
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
My c++
My c++My c++
My c++
 
Object in python tells about object oriented programming in python
Object in python tells about object oriented programming in pythonObject in python tells about object oriented programming in python
Object in python tells about object oriented programming in python
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 

Recently uploaded

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 

static-story.pdf

  • 1. The story of Static keywords’ behavior in Java Static is kind a opposite to Object Creation. Say you have a class person as below: class Person{ private int age; // suppose it has getters and setters } Object of Person can be created in the main class like this: Person p1 = new Person(); p1 is known as the object reference. The term hints on its bahaviour. It is a pointer (a variable that can hold memory addresses) to a memory location where object is situated. The memory where objects live is known as Heap. So object reference point to a memory address on the heap which houses an int age field. So in a way, you can say int age is the p1 object’s private data right? Make sure you understand this. Now lets suppose, it has another field say int count, forget about its purpose for now! So the new class would become: class Person{ private int age; static int count; // suppose it has getters and setters for both } Notice it is static! What does that imply. As described in the lecture. Static keyword changes the behavior of the field in focus i.e. int amount. It is no more part of the object’s private data. It also implies it is not on the heap right? As said during the lectures, it is actually associated with class. If you create several object from the class Person say p1, p2, p3. All of these may have their private data but do have the class Person in common. Static paves a way to share data among these objects by associating it with the class. So static features are associated with the class not with objects, memorize it now! It can be accessed in the main method of the Main class like this. I am not writing the whole main method’s signature for brevity.
  • 2. //inside main Person p1 = new Person(); p1.count; // what the hell!!! you said its not part of the object’s private data. Well, its still not part of the object’s private data and is not on the heap but java provides you with a way to still access it through the object reference. There is another way to access this, which is a better and more meaningful way i.e. access it through the class name. So we access class Person’s amount field like this: // inside Main class’s main method // i.e. outside the person class. Person.amount; As said earlier, all the object references p1, p2, p3 share a common class. The quantity of a class is one. It means you create multiple objects from one class. It also means class is written once for one or more objects to be created from it. So if you create an amount field, it will also be one since it is associated with class which is single (to be exact). Suppose you have a Person as written in the second example (i.e. OO and static fields) and you create three objects from it i.e. p1, p2, p3. How many occurrences of age variable would be? And what is the count of the amount field created in this scenario. Ans: 3 objects so 3 occurrences of age fields inside each object. and only occurrence of static field amount. In addition to fields, you can also have static method. Its access is again similar to the static field i.e. through class and object reference. You can also have a static block as shown below: class Person{ static{ sysout(“do whatever you like here”); } private int age; static int amount; Person(){} // suppose it has getters and setters for both }
  • 3. Static block runs even before the constructor, normally used to load a native routine in java. Or anything you wish to do once (as the class loads in JVM) even before the constructor runs. Another importance rule to mention here is static can only access another static feature of the class e.g. public class App { int a; public static void main( String[] args ){ System.out.println("trying to print a:"+a); } } Error arises as below: Cannot make a static reference to the non-static field a Solution 1 to the above problem is through object creation as a is an OO field. public class App { int a; public static void main( String[] args ){ App app = new App(); System.out.println("Access a like this:"+app.a); } } Solution 2, make the field static as well public class App { static int a; public static void main( String[] args ){ System.out.println("Access a by making it static inside main method " + "which is also static:"+a); } }