SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Static
Compiled By:
Vinod Kumar(Asst. Prof.)
Java static keyword
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.
• Java supports definition of global methods and variables that can be
accessed without creating objects of a class. Such members are called Static
members.
• Define a variable by marking with the static methods.
• This feature is useful when we want to create a variable common to all
instances of a class.
• Note: Java creates only one copy for a static variable which can be used
even if the class is never instantiated.
The static can be:
• variable (also known as class variable)
• method (also known as class method)
• block
• nested class
3
Java static variable
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.
Advantage of static variable
• It makes your program memory efficient (i.e. it saves memory).
class Student{
int rollno;
String name;
String university=“CURAJ";
}
Suppose there are 2000 students in our university, 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, university refers to the common
property of all objects . If we make it static , this field will get memory only once.
Example 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");
s1.display();
s2.display();
}
}
Program of counter without static variable and by
static variable
without static variable
class Counter{
int count=0;/*will get memory when instance is c
reated */
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
By static variable
class Counter2{
static int count=0;/*will get memory only once an
d 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();
}
}
6
Static Methods
• A static method belongs to the class rather than object of
a class.
• A class can have methods that are defined as static (e.g.,
main method).
• Static methods can be accessed without using objects.
Also, there is NO need to create objects.
• static method can access static data member and can
change the value of it.
• They are prefixed with keyword “static”
• Static methods are generally used to group related library
functions that don’t depend on data members of its class.
For example, Math library functions.
Example :1
class Student9{
int rollno;
String name;
static String university = "ITS";
static void change(){
university = “Curaj";
}
Student9(int r, String n){
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+university);
}
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();
}
}
Output:-
111 Karan Curaj
222 Aryan Curaj
333 Sonoo Curaj
Example: 2
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);
}
}
Output :
125
Comparator class with Static methods
/* Comparator.java: A class with static data
items comparison methods*/
class Comparator {
public static int max(int a, int b)
{
if( a > b)
return a;
else
return b;
}
public static String max(String a, String b)
{
if( a.compareTo (b) > 0)
return a;
else
return b;
}
}
class MyClass {
public static void main(String args[])
{
String s1 = "Melbourne";
String s2 = "Sydney";
String s3 = "Adelaide";
int a = 10;
int b = 20;
System.out.println(Comparator.max(a, b));
// which number is big
System.out.println(Comparator.max(s1, s2));
// which city is big
System.out.println(Comparator.max(s1, s3));
// which city is big
}
}
9
Directly accessed using
ClassName (NO Objects)
10
Static methods restrictions
• They can only call other static methods.
• They can only access static data.
• They cannot refer to “this” or “super” (more later) in anyway.
class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Output:
static block is invoked
Hello main
Can we execute a program without
main() method?
• 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);
}
}

More Related Content

PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PDF
Java keywords
PDF
Java variable types
PPS
Introduction to class in java
PDF
Java data types, variables and jvm
PPTX
Inner classes in java
PPTX
Access modifiers in java
PPTX
Java constructors
ABSTRACT CLASSES AND INTERFACES.ppt
Java keywords
Java variable types
Introduction to class in java
Java data types, variables and jvm
Inner classes in java
Access modifiers in java
Java constructors

What's hot (20)

PPTX
Strings in Java
PPTX
Constructor in java
PDF
Method Overloading In Java
PPTX
Method overloading
PPS
Wrapper class
PPTX
Type casting in java
PPT
Java interfaces
PPSX
Exception Handling
PPTX
6. static keyword
PPTX
Constructor in java
PPTX
Static Data Members and Member Functions
PPTX
Java abstract class & abstract methods
PPT
Final keyword in java
PPTX
Classes, objects in JAVA
PPTX
PDF
Arrays in Java
PPTX
Control structures in java
PPTX
Classes objects in java
PPT
Exception Handling in JAVA
PPTX
Data types in java
Strings in Java
Constructor in java
Method Overloading In Java
Method overloading
Wrapper class
Type casting in java
Java interfaces
Exception Handling
6. static keyword
Constructor in java
Static Data Members and Member Functions
Java abstract class & abstract methods
Final keyword in java
Classes, objects in JAVA
Arrays in Java
Control structures in java
Classes objects in java
Exception Handling in JAVA
Data types in java
Ad

Viewers also liked (20)

PPTX
Java static keyword
PPT
Java access modifiers
PDF
Top 371 java fa qs useful for freshers and experienced
PPT
Super keyword.23
PPTX
Super keyword in java
PPT
Client Side Programming with Applet
PPT
Selenium ide material (2)
PPT
Java static keyword
PPTX
Satish training ppt
PPT
Basics of applets.53
DOCX
Srgoc java
PPTX
Ppt on this and super keyword
PPT
L5 classes, objects, nested and inner class
PPTX
Applets in java
PPT
Fundamentals
PPT
Applet and graphics programming
PPTX
Super keyword in java
PPTX
Super keyword in java
PPTX
[OOP - Lec 01] Introduction to OOP
PPTX
Final keyword in java
Java static keyword
Java access modifiers
Top 371 java fa qs useful for freshers and experienced
Super keyword.23
Super keyword in java
Client Side Programming with Applet
Selenium ide material (2)
Java static keyword
Satish training ppt
Basics of applets.53
Srgoc java
Ppt on this and super keyword
L5 classes, objects, nested and inner class
Applets in java
Fundamentals
Applet and graphics programming
Super keyword in java
Super keyword in java
[OOP - Lec 01] Introduction to OOP
Final keyword in java
Ad

Similar to Static keyword ppt (20)

PPTX
Static keyword.pptx
PPTX
Lecture 6.pptx
PPTX
Lecture_4_Static_variables_and_Methods.pptx
PPTX
Static Members-Java.pptx
PPT
Java static keyword
PPTX
3.Classes&Objects.pptx
PPTX
11 static
PDF
OOPs & Inheritance Notes
PPT
Ch. 6 -Static Class Members.ppt programming
DOCX
Static variable
PPT
Static.18
PPTX
STATIC IN JAVA.pptx STATIC IN JaAVA.pptx
PPTX
BCA Class and Object (3).pptx
PPTX
This and Static Keyword
PPTX
Chap5java5th
PDF
03_Objects and Classes in java.pdf
PPTX
Week 3-LectureA Object Oriented Programmings.pptx
PPTX
Week10 packages using objects in objects
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PPTX
Class and Object.pptx from nit patna ece department
Static keyword.pptx
Lecture 6.pptx
Lecture_4_Static_variables_and_Methods.pptx
Static Members-Java.pptx
Java static keyword
3.Classes&Objects.pptx
11 static
OOPs & Inheritance Notes
Ch. 6 -Static Class Members.ppt programming
Static variable
Static.18
STATIC IN JAVA.pptx STATIC IN JaAVA.pptx
BCA Class and Object (3).pptx
This and Static Keyword
Chap5java5th
03_Objects and Classes in java.pdf
Week 3-LectureA Object Oriented Programmings.pptx
Week10 packages using objects in objects
Class and Object JAVA PROGRAMMING LANG .pdf
Class and Object.pptx from nit patna ece department

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
DevOps & Developer Experience Summer BBQ
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced IT Governance
PDF
KodekX | Application Modernization Development
20250228 LYD VKU AI Blended-Learning.pptx
DevOps & Developer Experience Summer BBQ
Chapter 2 Digital Image Fundamentals.pdf
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
SAP855240_ALP - Defining the Global Template PUBLIC.pdf
Advanced Soft Computing BINUS July 2025.pdf
Transforming Manufacturing operations through Intelligent Integrations
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
madgavkar20181017ppt McKinsey Presentation.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Monthly Chronicles - July 2025
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Smarter Business Operations Powered by IoT Remote Monitoring
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced IT Governance
KodekX | Application Modernization Development

Static keyword ppt

  • 2. Java static keyword 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. • Java supports definition of global methods and variables that can be accessed without creating objects of a class. Such members are called Static members. • Define a variable by marking with the static methods. • This feature is useful when we want to create a variable common to all instances of a class. • Note: Java creates only one copy for a static variable which can be used even if the class is never instantiated. The static can be: • variable (also known as class variable) • method (also known as class method) • block • nested class
  • 3. 3 Java static variable 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. Advantage of static variable • It makes your program memory efficient (i.e. it saves memory). class Student{ int rollno; String name; String university=“CURAJ"; } Suppose there are 2000 students in our university, 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, university refers to the common property of all objects . If we make it static , this field will get memory only once.
  • 4. Example 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"); s1.display(); s2.display(); } }
  • 5. Program of counter without static variable and by static variable without static variable class Counter{ int count=0;/*will get memory when instance is c reated */ Counter(){ count++; System.out.println(count); } public static void main(String args[]){ Counter c1=new Counter(); Counter c2=new Counter(); Counter c3=new Counter(); } } By static variable class Counter2{ static int count=0;/*will get memory only once an d 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(); } }
  • 6. 6 Static Methods • A static method belongs to the class rather than object of a class. • A class can have methods that are defined as static (e.g., main method). • Static methods can be accessed without using objects. Also, there is NO need to create objects. • static method can access static data member and can change the value of it. • They are prefixed with keyword “static” • Static methods are generally used to group related library functions that don’t depend on data members of its class. For example, Math library functions.
  • 7. Example :1 class Student9{ int rollno; String name; static String university = "ITS"; static void change(){ university = “Curaj"; } Student9(int r, String n){ rollno = r; name = n; } void display () { System.out.println(rollno+" "+name+" "+university); } 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(); } } Output:- 111 Karan Curaj 222 Aryan Curaj 333 Sonoo Curaj
  • 8. Example: 2 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); } } Output : 125
  • 9. Comparator class with Static methods /* Comparator.java: A class with static data items comparison methods*/ class Comparator { public static int max(int a, int b) { if( a > b) return a; else return b; } public static String max(String a, String b) { if( a.compareTo (b) > 0) return a; else return b; } } class MyClass { public static void main(String args[]) { String s1 = "Melbourne"; String s2 = "Sydney"; String s3 = "Adelaide"; int a = 10; int b = 20; System.out.println(Comparator.max(a, b)); // which number is big System.out.println(Comparator.max(s1, s2)); // which city is big System.out.println(Comparator.max(s1, s3)); // which city is big } } 9 Directly accessed using ClassName (NO Objects)
  • 10. 10 Static methods restrictions • They can only call other static methods. • They can only access static data. • They cannot refer to “this” or “super” (more later) in anyway. class A2{ static{System.out.println("static block is invoked");} public static void main(String args[]){ System.out.println("Hello main"); } } Output: static block is invoked Hello main
  • 11. Can we execute a program without main() method?
  • 12. • 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); } }