SlideShare a Scribd company logo
1 of 13
Java static keyword
Object Oriented Programming
Why main method (only) static in java?
how objects are created?
 Basic Steps of how objects are created
1. Class is loaded by JVM
2. Static variable and methods are loaded and initialized
and available for use
3. Constructor is called to instantiate the non static variables
4. Non static variables and methods are now available
• As all the non static variable are
available only after the constructor is
called, there is a restriction on using
non static variable in static methods.
what’s the use of static Keyword and
Why?
 The static keyword is used when a member variable of a class has to be
shared between all the instances of the class.
 All static variables and methods belong to the class and not to any
instance of the class
Why ?
 The static keyword in java is used for memory management mainly.
 Static keyword is used to save your time and typing .
Static and Non-static
 We can access static variables without creating an object of the class
 As they are already available at class loading time.
 We cannot use non static methods and variables without creating an
object of the class as they are bound to the instance of the class.
 They are initialized by the constructor when we create the object using
new operator.
The static can be:
static
Variables Methods Class Blocks Constructor!
Static Variables
 The static variable can be used to refer the common property of all objects
(that is not unique for each object).
 The static variable gets memory only once in class area at the time of class
loading.
 Advantage of static variable :-
It makes your program memory efficient (i.e. it saves memory).
Student8.java
Counter.java
Static Variables
Static Methods
 A static method belongs to the class rather than object of a class.
 A static method can be invoked without the need for creating an instance
of a class.
 static method can access static data member and can change the value
of it.
Student9.java
Calculate.java
Static Classes (Only Nested Class)
 A Class can be made static only if it is a nested Class.
 The nested static class can be accessed without having an object of outer
class.
StClass.java
Static Blocks
 Static block is mostly used for changing the default values of static
variables.
 This block gets executed when the class is loaded in the memory.
 A class can have multiple Static blocks, which will execute in the same
sequence in which they have been written into the program.
Stblocks.java
Stblocks2.java
Static Constructors
 they are not allowed in Java.
Why ??  Search.
The End
Thank You 

More Related Content

What's hot

Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarAbir Mohammad
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaManish Sahu
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Edureka!
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management DetailsAzul Systems Inc.
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersKhaled Adnan
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentalsjavaease
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 

What's hot (20)

Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management Details
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Java swing
Java swingJava swing
Java swing
 
java Features
java Featuresjava Features
java Features
 

Viewers also liked

Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 
03 objects and classes in java
03 objects and classes   in java03 objects and classes   in java
03 objects and classes in javaMrMazharJutt
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23myrajendra
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)Sriram Angajala
 
Performance management system slide
Performance management system slidePerformance management system slide
Performance management system slideraizanamiza
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part IIIHari Christian
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Presentation Portfolio
Presentation PortfolioPresentation Portfolio
Presentation PortfolioSteve Lee
 
Brown bag tfs best practices - slide share
Brown bag   tfs best practices - slide shareBrown bag   tfs best practices - slide share
Brown bag tfs best practices - slide shareJJ Bussert
 

Viewers also liked (20)

Java keywords
Java keywordsJava keywords
Java keywords
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
03 objects and classes in java
03 objects and classes   in java03 objects and classes   in java
03 objects and classes in java
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Fundamentals
FundamentalsFundamentals
Fundamentals
 
Performance management system slide
Performance management system slidePerformance management system slide
Performance management system slide
 
William Bronchick Coaching
William Bronchick CoachingWilliam Bronchick Coaching
William Bronchick Coaching
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Presentation Portfolio
Presentation PortfolioPresentation Portfolio
Presentation Portfolio
 
Brown bag tfs best practices - slide share
Brown bag   tfs best practices - slide shareBrown bag   tfs best practices - slide share
Brown bag tfs best practices - slide share
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

Similar to Java static keyword explained with examples

this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdfParvizMirzayev2
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questionsMehtaacademy
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics PresentationOmid Sohrabi
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfArpana Awasthi
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interviewKuntal Bhowmick
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptxAsifMulani17
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetdevlabsalliance
 

Similar to Java static keyword explained with examples (20)

this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
 
1
11
1
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
Java Core
Java CoreJava Core
Java Core
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

Java static keyword explained with examples

  • 1. Java static keyword Object Oriented Programming
  • 2. Why main method (only) static in java?
  • 3. how objects are created?  Basic Steps of how objects are created 1. Class is loaded by JVM 2. Static variable and methods are loaded and initialized and available for use 3. Constructor is called to instantiate the non static variables 4. Non static variables and methods are now available • As all the non static variable are available only after the constructor is called, there is a restriction on using non static variable in static methods.
  • 4. what’s the use of static Keyword and Why?  The static keyword is used when a member variable of a class has to be shared between all the instances of the class.  All static variables and methods belong to the class and not to any instance of the class Why ?  The static keyword in java is used for memory management mainly.  Static keyword is used to save your time and typing .
  • 5. Static and Non-static  We can access static variables without creating an object of the class  As they are already available at class loading time.  We cannot use non static methods and variables without creating an object of the class as they are bound to the instance of the class.  They are initialized by the constructor when we create the object using new operator.
  • 6. The static can be: static Variables Methods Class Blocks Constructor!
  • 7. Static Variables  The static variable can be used to refer the common property of all objects (that is not unique for each object).  The static variable gets memory only once in class area at the time of class loading.  Advantage of static variable :- It makes your program memory efficient (i.e. it saves memory). Student8.java Counter.java
  • 9. Static Methods  A static method belongs to the class rather than object of a class.  A static method can be invoked without the need for creating an instance of a class.  static method can access static data member and can change the value of it. Student9.java Calculate.java
  • 10. Static Classes (Only Nested Class)  A Class can be made static only if it is a nested Class.  The nested static class can be accessed without having an object of outer class. StClass.java
  • 11. Static Blocks  Static block is mostly used for changing the default values of static variables.  This block gets executed when the class is loaded in the memory.  A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program. Stblocks.java Stblocks2.java
  • 12. Static Constructors  they are not allowed in Java. Why ??  Search.

Editor's Notes

  1. //Program of static variable class Student8 { int rollno; String name; static String college ="ITS"; Student8(int r,String n) { rollno = r; name = n; } void display () { System.out.println(rollno+" "+name+" "+college); } public static void main(String args[]) { Student8 s1 = new Student8(111,"Karan"); Student8 s2 = new Student8(222,"Aryan"); System.out.println(college); s1.display(); s2.display(); } } ======================================= class Counter{ static int count=0;//will get memory when instance is created and try without static to see the difference Counter(){ count++; System.out.println(count); } public static void main(String args[]){ Counter c1=new Counter(); Counter c2=new Counter(); Counter c3=new Counter(); } }
  2. //Program of changing the common property of all objects(static field). class Student9{ int rollno; String name; static String college = "ITS"; static void change(){ college = "BBDIT"; } Student9(int r, String n){ rollno = r; name = n; } static void display (){System.out.println(rollno+" "+name+" "+college);} public static void main(String args[]){ change(); Student9 s1 = new Student9 (111,"Karan"); Student9 s2 = new Student9 (222,"Aryan"); Student9 s3 = new Student9 (333,"Sonoo"); s1.display(); s2.display(); s3.display(); } } ===================================== //Program to get cube of a given number by static method class Calculate{ static int cube(int x){ return x*x*x; } public static void main(String args[]){ int result=Calculate.cube(5); System.out.println(result); } }
  3. /*A Class can be made static only if it is a nested Class. //The nested static class can be accessed without having an object of outer class.*/ class stClass{ //Static class class X{ String str ="Inside Class X"; } public static void main(String args[]) { stClass.X.str=“ahmed"; X.str="Inside Class Example1"; System.out.println("String stored in str is- "+ X.str); } }
  4. class Stblocks{ static int num=5; static String mystr; static{ num = 97; mystr = "Static keyword in Java"; } public static void main(String args[]) { System.out.println("Value of num="+num); System.out.println("Value of mystr="+mystr); } } =========================================== class Stblocks2{ static int num; static String mystr; //First Static block static{ System.out.println("Static Block 1"); num = 68; mystr = "Block1"; } //Second static block static{ System.out.println("Static Block 2"); num = 98; mystr = "Block2"; } public static void main(String args[]) { System.out.println("Value of num="+num); System.out.println("Value of mystr="+mystr); } }