SlideShare a Scribd company logo
 Static variable
 Program of counter without static variable
 Program of counter with static variable
 Static method
 Restrictions for static method
 Why main method is static ?
 Static block
 Can we execute a program without main
method ?
 The static keyword in java is used for memory
management mainly. We can apply java static
keyword with variables, methods, blocks and
nested class. The static keyword belongs to
the class than instance of the class.
 The static can be:
◦ variable (also known as class variable)
◦ method (also known as class method)
◦ block
◦ nested class
 If you declare any variable as static, it is
known static variable.
◦ The static variable can be used to refer the common
property of all objects (that is not unique for each
object) e.g. company name of employees,college
name of students etc.
◦ The static variable gets memory only once in class
area at the time of class loading.
 It makes your program memory efficient (i.e
it saves memory).
 class Student{
 int rollno;
 String name;
 String college="ITS";
 }
 Suppose there are 500 students in my college,
now all instance data members will get memory
each time when object is created.All student have
its unique rollno and name so instance data
member is good.Here, college refers to the
common property of all objects.If we make it
static,this field will get memory only once.
 Java static property is shared to all objects.
 //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+" "+co
llege);}


 public static void main(String args[]){
 Student8 s1 = new Student8(111,"Karan");
 Student8 s2 = new Student8(222,"Aryan");

 s1.display();
 s2.display();
 }
 }
 In this example, we have created an instance
variable named count which is incremented in
the constructor. Since instance variable gets
the memory at the time of object creation,
each object will have the copy of the instance
variable, if it is incremented, it won't reflect
to other objects. So each objects will have the
value 1 in the count variable.
 class Counter{
 int count=0;//will get memory when instance is created

 Counter(){
 count++;
 System.out.println(count);
 }

 public static void main(String args[]){

 Counter c1=new Counter();
 Counter c2=new Counter();
 Counter c3=new Counter();

 }
 }
 As we have mentioned above, static variable
will get the memory only once, if any object
changes the value of the static variable, it will
retain its value.
 class Counter2{
 static int count=0;//will get memory only once and retain its value

 Counter2(){
 count++;
 System.out.println(count);
 }

 public static void main(String args[]){

 Counter2 c1=new Counter2();
 Counter2 c2=new Counter2();
 Counter2 c3=new Counter2();

 }
 }
 If you apply static keyword with any method,
it is known as static method.
◦ 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.
 //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;
 }


 void display (){System.out.println(rollno+" "+name+" "+college);}

 public static void main(String args[]){
 Student9.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 m
ethod

 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);
 }
 }
 There are two main restrictions for the static
method. They are:
◦ 1. The static method can not use non static data
member or call non-static method directly.
◦ 2. this and super cannot be used in static context.
 class A{
 int a=40;//non static

 public static void main(String args[]){
 System.out.println(a);
 }
 }
 Ans) because object is not required to call
static method if it were non-static method,
jvm create object first then call main()
method that will lead the problem of extra
memory allocation.
 Is used to initialize the static data member.
 It is executed before main method at the time
of classloading.
 class A2{
 static{System.out.println("static block is invo
ked");}
 public static void main(String args[]){
 System.out.println("Hello main");
 }
 }
 Output:static block is invoked
 Hello main
 Ans) Yes, one of the way is static block but in
previous version of JDK not in JDK 1.7.
 class A3{
 static{
 System.out.println("static block is invoked");
 System.exit(0);
 }
 }
 Output:static block is invoked (if not JDK7)
 In JDK7 and above, output will be:
 Output:Error: Main method not found in class
A3, please define the main method as: public
static void main(String[] args)

More Related Content

What's hot

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Lovely Professional University
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
Anwar Hasan Shuvo
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 

What's hot (20)

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java exception
Java exception Java exception
Java exception
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 

Viewers also liked

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
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Java(Access Modifiers)
Java(Access Modifiers)Java(Access Modifiers)
Java(Access Modifiers)
Shridhar Ramesh
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Sourabrata Mukherjee
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in javaTech_MX
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Muthukumaran Subramanian
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 

Viewers also liked (20)

Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
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
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Java(Access Modifiers)
Java(Access Modifiers)Java(Access Modifiers)
Java(Access Modifiers)
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Java packages
Java packagesJava packages
Java packages
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 

Similar to 6. static keyword

Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
KishanMishra44
 
Static variable
Static  variableStatic  variable
Static variable
vishal choudhary
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
Infoviaan Technologies
 
Java Programs
Java ProgramsJava Programs
Java Programs
vvpadhu
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
BG Java EE Course
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Gandhi Ravi
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
final year project center in Coimbatore
final year project center in Coimbatorefinal year project center in Coimbatore
final year project center in Coimbatore
cbeproject centercoimbatore
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
PawanMM
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
Nagaraju Pamarthi
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Java session5
Java session5Java session5
Java session5
Jigarthacker
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
KUNALHARCHANDANI1
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 

Similar to 6. static keyword (20)

Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
 
Static variable
Static  variableStatic  variable
Static variable
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
final year project center in Coimbatore
final year project center in Coimbatorefinal year project center in Coimbatore
final year project center in Coimbatore
 
Chap08
Chap08Chap08
Chap08
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Oop
OopOop
Oop
 
Java session5
Java session5Java session5
Java session5
 
Static Keyword Static is a keyword in C++ used to give special chara.pdf
  Static Keyword Static is a keyword in C++ used to give special chara.pdf  Static Keyword Static is a keyword in C++ used to give special chara.pdf
Static Keyword Static is a keyword in C++ used to give special chara.pdf
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 

More from Indu Sharma Bhardwaj

E model
E modelE model
E commerce
E commerceE commerce
Ui design final
Ui design finalUi design final
Ui design final
Indu Sharma Bhardwaj
 
Testing
TestingTesting
Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
Indu Sharma Bhardwaj
 
Software project management 3
Software project management 3Software project management 3
Software project management 3
Indu Sharma Bhardwaj
 
Software project management
Software project managementSoftware project management
Software project management
Indu Sharma Bhardwaj
 
Software process and project metrics
Software process and project metricsSoftware process and project metrics
Software process and project metrics
Indu Sharma Bhardwaj
 
Software maintenance
Software maintenanceSoftware maintenance
Software maintenance
Indu Sharma Bhardwaj
 
Software resuse
Software  resuseSoftware  resuse
Software resuse
Indu Sharma Bhardwaj
 
Risk analysis
Risk analysisRisk analysis
Risk analysis
Indu Sharma Bhardwaj
 
Design final
Design finalDesign final
Design final
Indu Sharma Bhardwaj
 
Debugging
DebuggingDebugging
10 common english mistakes
10 common english mistakes10 common english mistakes
10 common english mistakes
Indu Sharma Bhardwaj
 
4. method overloading
4. method overloading4. method overloading
4. method overloading
Indu Sharma Bhardwaj
 
2. hello java
2. hello java2. hello java
2. hello java
Indu Sharma Bhardwaj
 

More from Indu Sharma Bhardwaj (18)

E model
E modelE model
E model
 
E commerce
E commerceE commerce
E commerce
 
Ui design final
Ui design finalUi design final
Ui design final
 
Testing
TestingTesting
Testing
 
Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
 
Software project management 3
Software project management 3Software project management 3
Software project management 3
 
Software project management
Software project managementSoftware project management
Software project management
 
Software process and project metrics
Software process and project metricsSoftware process and project metrics
Software process and project metrics
 
Software maintenance
Software maintenanceSoftware maintenance
Software maintenance
 
Software resuse
Software  resuseSoftware  resuse
Software resuse
 
Risk analysis
Risk analysisRisk analysis
Risk analysis
 
Design final
Design finalDesign final
Design final
 
Debugging
DebuggingDebugging
Debugging
 
10 common english mistakes
10 common english mistakes10 common english mistakes
10 common english mistakes
 
3. jvm
3. jvm3. jvm
3. jvm
 
4. method overloading
4. method overloading4. method overloading
4. method overloading
 
2. hello java
2. hello java2. hello java
2. hello java
 
1 .java basic
1 .java basic1 .java basic
1 .java basic
 

Recently uploaded

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

6. static keyword

  • 1.  Static variable  Program of counter without static variable  Program of counter with static variable  Static method  Restrictions for static method  Why main method is static ?  Static block  Can we execute a program without main method ?
  • 2.  The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
  • 3.  The static can be: ◦ variable (also known as class variable) ◦ method (also known as class method) ◦ block ◦ nested class
  • 4.  If you declare any variable as static, it is known static variable. ◦ The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc. ◦ The static variable gets memory only once in class area at the time of class loading.
  • 5.  It makes your program memory efficient (i.e it saves memory).
  • 6.  class Student{  int rollno;  String name;  String college="ITS";  }  Suppose there are 500 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once.  Java static property is shared to all objects.
  • 7.  //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+" "+co llege);} 
  • 8.   public static void main(String args[]){  Student8 s1 = new Student8(111,"Karan");  Student8 s2 = new Student8(222,"Aryan");   s1.display();  s2.display();  }  }
  • 9.
  • 10.  In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.
  • 11.  class Counter{  int count=0;//will get memory when instance is created   Counter(){  count++;  System.out.println(count);  }   public static void main(String args[]){   Counter c1=new Counter();  Counter c2=new Counter();  Counter c3=new Counter();   }  }
  • 12.  As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
  • 13.  class Counter2{  static int count=0;//will get memory only once and retain its value   Counter2(){  count++;  System.out.println(count);  }   public static void main(String args[]){   Counter2 c1=new Counter2();  Counter2 c2=new Counter2();  Counter2 c3=new Counter2();   }  }
  • 14.  If you apply static keyword with any method, it is known as static method. ◦ 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.
  • 15.  //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;  }  
  • 16.  void display (){System.out.println(rollno+" "+name+" "+college);}   public static void main(String args[]){  Student9.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();  }  }
  • 17.  //Program to get cube of a given number by static m ethod   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);  }  }
  • 18.  There are two main restrictions for the static method. They are: ◦ 1. The static method can not use non static data member or call non-static method directly. ◦ 2. this and super cannot be used in static context.  class A{  int a=40;//non static   public static void main(String args[]){  System.out.println(a);  }  }
  • 19.  Ans) because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.
  • 20.  Is used to initialize the static data member.  It is executed before main method at the time of classloading.
  • 21.  class A2{  static{System.out.println("static block is invo ked");}  public static void main(String args[]){  System.out.println("Hello main");  }  }  Output:static block is invoked  Hello main
  • 22.  Ans) Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.  class A3{  static{  System.out.println("static block is invoked");  System.exit(0);  }  }  Output:static block is invoked (if not JDK7)
  • 23.  In JDK7 and above, output will be:  Output:Error: Main method not found in class A3, please define the main method as: public static void main(String[] args)