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

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptxParvizMirzayev2
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Packages in java
Packages in javaPackages in java
Packages in javajamunaashok
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in javaHrithikShinde
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarAbir Mohammad
 
L11 array list
L11 array listL11 array list
L11 array listteach4uin
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 

What's hot (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
 
L11 array list
L11 array listL11 array list
L11 array list
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

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
 
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
 

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)
 
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
 
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)
 

Similar to Java static keyword

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
 
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
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 

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
 
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
 
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
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 

Recently uploaded

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 

Recently uploaded (20)

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 

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

  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); } }