Chapter 5-4 OOP Static Part2
Chapter 5-4 OOP Static Part2
1
What is static
The keyword static means that a particular member
belongs to a type (class) itself, rather than to an
instance of that type.
This means we'll create only one instance of that
static member (attribute/method) that is shared
across all instances of the class.
The static keyword in Java is used for memory
management mainly.
2
When can we access static
variable
When a class is loaded by the virtual machine all the
static variables and methods are available for use.
3
Static Variables
The static variable can be used to
refer to the common property of all
objects (which is not unique for each
Advantages of
object), for example, the company static variable:
name for Employee class in X
company.
The static variable gets memory only
It makes your
once in the class area at the time of
class loading.
program memory
efficient (i.e., it
saves memory).
4
Static vs Non-static Example 1
Suppose we have 500 students in this
college, now all attributes will get
memory each time when the object is
created including college.
All students have its unique rollno and
name, so creating them as non-static
members is wise decision.
“IT”
5
Static vs Non-static Example 1
class Student { // Test class to show the values of
int rollno;// instance variable objects
String name; public class TestStaticVariable1 {
static String college = "IT";// static public static void main(String args[])
variable {
// constructor Student s1 = new Student(111,
Student(int r, String n) { "Ahmed");
rollno = r; Student s2 = new Student(222,
name = n; "Mohamed");
} // we can change the college of
// method to display the values all objects by the single line of code
void display() { // Student.college="BBDIT";
System.out.println(rollno + " " + s1.display();
name + " " + college); s2.display();
} }
} }
6
Memory illustration-Example 1
College = “IT”
Class Area
id= 222
name= “Ahmed”
id= 333
S2 name= “Khouloud”
Heap Memory
Stack S1
Memory
7
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.
How it works
BASIC STEPS OF HOW OBJECTS
ARE CREATED
Constructor is called to
instantiate the non static
variables
8
Example 2- Counter without
static variable
class Counter {
int count = 0;
// will get memory each time when the instance is created
Counter() {
count++; // incrementing value
System.out.println(count);
}
public static void main(String args[]) {
// Creating objects
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}
9
Example 2- Counter with static
variable
class Counter {
static int count = 0; //
will get memory only once and retain its value
Counter() {
count++;// incrementing the value of static variable
System.out.println(count);
}
public static void main(String args[]) {
// creating objects
Counte c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}s
10
Static Methods
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.
11
Static Methods
static non-static
method method
only access static access static data
data members and members and static
static methods of methods as well as
rewrite the values change the values
the same class or non-static
of any static data of any static data
another class but members and
member. member
cannot access non- methods of
static methods and another class or
variables. same class
12
Static Method Example
class Student { //Test class
int rollno; public class TestStaticMethod {
String name; public static void main(String args[]) {
static String college = "IT"; // calling change method
// Student.change();
static method to change the value of static var // creating objects
iable Student s1 = new Student(111, "Karan");
static void change() { Student s2 = new Student(222, "Aryan");
college = "BBDIT"; Student s3 = new Student(333, "Sonoo");
} // calling display method
// constructor to initialize the variable s1.display();
Student(int r, String n) { s2.display();
rollno = r; s3.display();
name = n; }
} }
// method to display values
void display() {
System.out.println(rollno + " " + name
+ " " + college);
}
}
13
Static Methods Restrictions
The static method can
not use non static data
member or call non-
static method directly.
14
Why is Java main method
static?
It is because the object is not required to call
a static method.
15
Why do we need this?
Static methods are identified to be
mostly used when we are writing
any utility methods.
We use static variables when
sharing data.
16