0% found this document useful (0 votes)
8 views

4 Static in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

4 Static in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

JAVA STATIC KEYWORD

INTRODUCTION TO JAVA PROGRAMMING

CSA6003T
BHAWANA BOTHARA
ASSISTANT PROFESSOR
LEARNING OBJECTIVES

• Understand the purpose and usage of the static keyword in Java.


• Differentiate between static and non-static members.
• Implement static methods and variables in Java programs.
• Understand real-world applications of static members.
• Troubleshoot common issues related to static usage in Java.
INTRODUCTION

• The static keyword in Java is used for memory management mainly. We can
apply static keyword with variables, methods, blocks and nested classes. The
static keyword belongs to the class than an instance of the class.
THE STATIC CAN BE:

1. Variable (also known as a class variable)


2. Method (also known as a class method)
3. Block
4. Nested class
1) JAVA STATIC VARIABLE

• If you declare any variable as static, it is known as a static variable.


• The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
• The static variable gets memory only once in the class area at the time of
class loading.
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
JAVA STATIC METHOD
• When a method is declared with the static keyword, it is known as the static
method. The most common example of a static method is the main( ) method.
As discussed above, Any static member can be accessed before any objects
of its class are created, and without reference to any object. Methods
declared as static have several restrictions:
• If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a class.
• A static method can access static data member and can change the value of it.
RESTRICTIONS FOR THE STATIC METHOD

• There are two main restrictions for the static method. They are:
• The static method can not use non static data member or call non-static method directly.
• this and super cannot be used in static context.
public class MathUtils {

// static method to calculate the factorial of


a number
public static int factorial(int n) {
// base case
if (n == 0 || n == 1) { public class Main {
return 1;
} public static void main(String[] args) {
// recursive case
return n * factorial(n - 1); // use the static methods of the MathUtils
} class
System.out.println("Factorial of 5: " +
// static method to calculate the power of a MathUtils.factorial(5));
number System.out.println("Power of 2 to the 3: "
public static int power(int x, int n) { + MathUtils.power(2, 3));
// base case }
if (n == 0) { }
return 1;
}
// recursive case
return x * power(x, n - 1);
}
}
BENEFITS OF STATIC METHODS

• Some of the benefits of using static methods are:


• They can be used to provide utility or helper methods that do not depend on the state of
the object, such as mathematical operations, string manipulation, etc.
• They can be used to access or modify static variables of the class, as they are in the
same scope.
• They can be accessed without creating an object of the class, which can be useful for
performance or convenience reasons.
JAVA STATIC BLOCK
• A static block is a block of code that is executed only once when the class is
loaded. It is also known as a static initializer. A static block is declared using
the static keyword before the opening curly brace, as shown below:

static { // block of code}


class Example {
static {
System.out.println("Static block 1");
}
static {
System.out.println("Static block 2");
} Static block 1
} Static block 2
public class Test {
public static void main(String[] args) {
Example obj = new Example();
}
}
COMMON PITFALLS

• Overuse of static methods and variables can lead to code that is difficult to
maintain and test.
• Static methods cannot be overridden.
• Static methods cannot access instance variables directly.
• Misunderstanding the lifecycle and scope of static members.
REVIEW AND SUMMARY

• The static keyword is used for memory management and belongs to the
class.Static variables and methods can be accessed without creating an
instance.Static blocks are used for static initialization.Understand the
differences between static and non-static members.Be mindful of common
pitfalls associated with static usage.
ASSESSMENT
1. What is true about static variables?
a) They are shared among all instances of a class.
b) They are created with each new instance.
c) They can be accessed only through objects.
d) None of the above.

2. Static methods can be called without __________ an instance of the class.


Answer: creating
3. Can static methods access instance variables directly?
a) Yes
b) No
ASSESSMENT
1. Static blocks are executed when the class is __________. Answer: loaded
into memory
2. Which of the following is a correct way to call a static method?
a) object.method()
b) class.method()
ASSIGNMENT

• What is static in Java?


• What is a static method in Java?
• What is a static variable in Java?
• How to call a static method in Java?
• Why is the main method in Java declared as static?

You might also like