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

More Related Content

What's hot (20)

Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
Infoviaan Technologies
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
vinay arora
 
Strings
StringsStrings
Strings
naslin prestilda
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
Mahika Tutorials
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
myrajendra
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Hitesh Kumar
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Main method in java
Main method in javaMain method in java
Main method in java
Hitesh Kumar
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Abstract class
Abstract classAbstract class
Abstract class
Tony Nguyen
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
PravinYalameli
 
OOPS In JAVA.pptx
OOPS In JAVA.pptxOOPS In JAVA.pptx
OOPS In JAVA.pptx
Sachin33417
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 

Viewers also liked (20)

Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
tanu_jaswal
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
Jussi Pohjolainen
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Srinivas Reddy
 
03 objects and classes in java
03 objects and classes   in java03 objects and classes   in java
03 objects and classes in java
MrMazharJutt
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
myrajendra
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)
Sriram Angajala
 
Fundamentals
FundamentalsFundamentals
Fundamentals
myrajendra
 
Performance management system slide
Performance management system slidePerformance management system slide
Performance management system slide
raizanamiza
 
William Bronchick Coaching
William Bronchick CoachingWilliam Bronchick Coaching
William Bronchick Coaching
WilliamBronchick123
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Luis Goldster
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh 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 III
Hari Christian
 
Presentation Portfolio
Presentation PortfolioPresentation Portfolio
Presentation Portfolio
Steve 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 share
JJ Bussert
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Sermon Slide Deck: "The Best Present Ever" (John 3:16)
Sermon Slide Deck: "The Best Present Ever" (John 3:16)Sermon Slide Deck: "The Best Present Ever" (John 3:16)
Sermon Slide Deck: "The Best Present Ever" (John 3:16)
New City Church
 
Talent management strategy
Talent management strategyTalent management strategy
Talent management strategy
Eric Tachibana
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
tanu_jaswal
 
03 objects and classes in java
03 objects and classes   in java03 objects and classes   in java
03 objects and classes in java
MrMazharJutt
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
myrajendra
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh 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 slide
raizanamiza
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh 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 III
Hari Christian
 
Presentation Portfolio
Presentation PortfolioPresentation Portfolio
Presentation Portfolio
Steve 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 share
JJ Bussert
 
Sermon Slide Deck: "The Best Present Ever" (John 3:16)
Sermon Slide Deck: "The Best Present Ever" (John 3:16)Sermon Slide Deck: "The Best Present Ever" (John 3:16)
Sermon Slide Deck: "The Best Present Ever" (John 3:16)
New City Church
 
Talent management strategy
Talent management strategyTalent management strategy
Talent management strategy
Eric Tachibana
 
Ad

Similar to Java static keyword (20)

this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
ParvizMirzayev2
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
Dhrumil Panchal
 
Understanding the Static Keyword in C#: A Beginner’s Guide
Understanding the Static Keyword in C#: A Beginner’s GuideUnderstanding the Static Keyword in C#: A Beginner’s Guide
Understanding the Static Keyword in C#: A Beginner’s Guide
StudySection
 
1
11
1
ksuthesan
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
Mehtaacademy
 
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
MaheshRamteke3
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
venud11
 
Java Core
Java CoreJava Core
Java Core
Gaurav Mehta
 
Presentation2.ppt java basic core ppt .
Presentation2.ppt  java basic core ppt .Presentation2.ppt  java basic core ppt .
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
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
Sagar 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 job
Garuda Trainings
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
Ahmad sohail Kakar
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
Gaurav Mehta
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
Arpana Awasthi
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
Ghulam Abbas Khan
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
AsifMulani17
 
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
Hari kiran G
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
ParvizMirzayev2
 
Understanding the Static Keyword in C#: A Beginner’s Guide
Understanding the Static Keyword in C#: A Beginner’s GuideUnderstanding the Static Keyword in C#: A Beginner’s Guide
Understanding the Static Keyword in C#: A Beginner’s Guide
StudySection
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
Mehtaacademy
 
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
100 Java questions FOR LOGIC BUILDING SOFTWARE.docx
MaheshRamteke3
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
venud11
 
Presentation2.ppt java basic core ppt .
Presentation2.ppt  java basic core ppt .Presentation2.ppt  java basic core ppt .
Presentation2.ppt java basic core ppt .
KeshavMotivation
 
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
Sagar 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 job
Garuda Trainings
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
Omid Sohrabi
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
Gaurav Mehta
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
Arpana Awasthi
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
Ghulam Abbas Khan
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
AsifMulani17
 
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
Hari kiran G
 
Ad

Recently uploaded (20)

The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
Apple Logic Pro X Crack FRESH Version 2025
Apple Logic Pro X Crack FRESH Version 2025Apple Logic Pro X Crack FRESH Version 2025
Apple Logic Pro X Crack FRESH Version 2025
fs4635986
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
Lionel Briand
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Cryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptxCryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptx
riyageorge2024
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
Apple Logic Pro X Crack FRESH Version 2025
Apple Logic Pro X Crack FRESH Version 2025Apple Logic Pro X Crack FRESH Version 2025
Apple Logic Pro X Crack FRESH Version 2025
fs4635986
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
FlakyFix: Using Large Language Models for Predicting Flaky Test Fix Categorie...
Lionel Briand
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
Cryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptxCryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptx
riyageorge2024
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 

Java static keyword

  • 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

  • #8: //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(); } }
  • #10: //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); } }
  • #11: /*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); } }
  • #12: 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); } }