Java Static
keyword, Static method and Static block........
Questions.......??
1) What is static keyword, static method(), static block
2) Define static keyword, static method(), static block
3) What do you mean by static keyword, static method(), static
block
4)static keyword, static method(), static block in
Java ?
1)Static Keyword:-
Static keyword in Java Programming language is
used for memory management purpose, it indicates that a particular member
belong to a type itself, rather than to an instance of that type. As we
mentioned above that is used for memory management purpose it means that static
keyword gets memory once in the class which help in reduction of the memory,
which makes the execution speed of the program fast which is a great advantage
of the static keyword.
let's take example how static keyword works.....
firstly we will see how memory get wasted when we
didn't use the static keyword.
class company
{
int
emp_id; // instance variable
String emp_name; // instance variable
String company_name; // instance variable
company(int id, String name, String cname) // 3 parameterized constructor..
{
emp_id=id;
emp_name=name;
company_name=name;
}
void display()
// class method..
{
System.out.println(emp_id+"
"+emp_name+" "+company_name);
}
public static void main(String[] args) // main method.
{
company c1=new
company(1,"aaa","XYZ"); // object created by allocating new
memory
company c2=new
company(1,"bbb","XYZ"); // object created by allocating new
memory
c1.display(); // calling class method by object
c2.display(); // calling class method by object
}
}
Output:-
1 aaa XYZ
2 bbb XYZ
Explanation:- In the above given program we have the class
with the name as company in that we have instance variable with the data type
as integer and string, then we have parameterized constructor in which we have
passed the variable then we have the method named as display in which we have
the printing statement in which we will print the value of employee id,
employee name and company name, then we have the main method in which we have
created the object by allocating the new memory, then we have called the display
method with the help of the object.
In this program we have the company name same as
'XYZ' so here we have passed the same value to the given variable in this case
we have done wastage of the memory. To overcome this problem static keyword
is been used.
let's see the example how to overcome the problem
of wastage of memory
class company
{
int
emp_id; //
instance variable
String emp_name; // instance variable
static
String company_name="XYZ";
// static variable
company(int id, String name) //
parameterized constructor..
{
emp_id=id;
emp_name=name;
}
void display()
// class method..
{
System.out.println(emp_id+"
"+emp_name+" "+company_name);
}
public static void main(String[] args) // main method.
{
company c1=new company(1,"aaa"); // object created by allocating new
memory
company c2=new company(1,"bbb"); // object created by allocating new
memory
c1.display(); // calling class method by object
c2.display(); // calling class method by object
}
}
Output:-
1 aaa XYZ
2 bbb XYZ
Explanation:- In the above given program we have the class
with the name as company in that we have instance variable with the data type
as integer and string and the static variable with the data type string
then we have parameterized constructor in which we have passed the variable
then we have the method named as display in which we have the printing
statement in which we will print the value of employee id, employee name and
company name, then we have the main method in which we have created the object
by allocating the new memory, then we have called the diplay method with the
help of the object. Here we used the static variable in which the company name
is already declared. So, the memory is not wasted.
---------------------------------------------------------------------------------------------------------------------------
2)Static method():-
Whenever a Static keyword is used before a
method then the method is changed to static method or it is called as Static
method. The purpose of the static method is same as of the static
keyword that it help's in memory management, static method can only
access the static variable here in static method the memory
management is done in such a manner that we didn't have to create the object we
can directly call the method with the name of the class, as the object is not
created memory didn't get wasted and the execution of the program speeds up.
Let's understand with one example
class rectangle_and_square
{
static int square(int n)
{
return n*n;
}
static int rectangle(int length,int breadth)
{
return length*breadth;
}
public static void main(String[] args)
{
System.out.println("Area of square is
:"+rectangle_and_square.square(5));
System.out.println("Area of rectangle is
:"+rectangle_and_square.rectangle(5,4));
}
}
Output:-
Area of square is :20
Area of rectangle is :25
Explanation:- In the above example we have used the class as
rectangle_and_square in which we used the static method() for finding the area
of rectangle and square firstly we used static method in which we passed single
value and return area of square, then we used static method in which we passed
two value and return area of rectangle, then we have the main method in which
we have two printing statement in which we called the respective method with
the help of the name of the class without creating the object in this way
memory is saved and execution of the program boost's..
---------------------------------------------------------------------------------------------------------------------------
3) Static block:-
The static block is a block of the statement
inside a java class that will be executed when a class is first loaded into the
JVM. A static block helps to initialize the static data members, just
like constructors help to initialize instance members. It is always used before
the main method at the time of the class loading.
Lets see an example of static block
class sample
{
static
{
System.out.println("Static block is
invoked");
}
public static void main(String[] args)
{
System.out.println("Main method is
invoked");
}
}
Output:-
Static block is invoked
Main method is invoked
Explanation:- In the above example we have the class with name
sample in which we executed the static block in that static block we the printing statement that is static block
is invoked after that we have the main method in which we have the printing
statement that is Main method is invoked.
---------------------------------------------------------------------------------------------------------------------------
Good Job Sir My doubt are clear after learning ur blog
ReplyDeleteThanks buddy ☺️😄
DeleteThe explanation is too good and easy to understand sir...keep it up👍👍
ReplyDeleteThanks buddy keep supporting
DeleteThat work was really good sir , it really helps lot of us ... Keep going ✨
ReplyDelete