Scientech Easy Static Method in Java with Example & Programs | Scientech Easy

Thursday, September 13, 2018

Static Method in Java with Example & Programs

As we have familiarized from Static keyword in the previous tutorial. So in this tutorial, we will learn Static method with practical example and programs in an easy and step by step. 

What is a Static Method in Java?


➲ If you declare a static keyword with any method, it is called the static method.
For example
     public static void msg() // Public is the modifier.
     { // block start
       - - - - - - --  -
       // Method body.
      - - - - - - - - - 
        } // block end
You can see the different form of declarations of the static method in the below screenshots.
java static methods, static method in java with example, what is static method in java, and Difference between static method and instance method.
Static method declaration

➲ It is also known as a class method because it belongs to a class rather than the object of a class.
➲ We do not need to create the object of the class for accessing the static method. It can be called directly within the same class without using the class name but in other class, we can call using the class name.
For example
    Student.add(); // Student is the name of class and add is a method.
➲ When we create a static method in the class, Only one copy of the method is created in the memory and shared by all objects of the class. Whether you create 100 objects or 1 object. 
➲ A static method is also loaded into the memory before the object creation.
➲ Only the static variable can be accessed inside the static method. We cannot access the instance variables inside the static method but a static variable can be accessed inside the instance area(instance method).
Let's see a simple example to understand the concept.
Program source code 1:
    package com.scientecheasy; public class StaticTest { // Instance Area. static int x=20; // static variable int y=30; // instance variable // Declare an instance method. void display(){ // Instance area. So we can call instance variable directly without object reference variable. System.out.println(x); // Since we can access static member within instance area. Therefore, we can call the static variable directly. System.out.println(y); } // Declare a static method. static void show(){ // Static Area. So we can call S.V. directly inside the S.M. System.out.println(x); // System.out.println(y); // compile time error because N.S.V. cannot access inside S.M. } public static void main(String[] args) { // Create the object of the class. StaticTest st=new StaticTest(); // Call the I.M. using reference variable st. st.display(); // Call the S.M. show(); } }
    Output: 20 30 20

➲ The static method can be accessed by nullable reference as like
    StaticMethod s1=null;
     s1.show();
Program source code 2:
    package com.scientecheasy; public class StaticMethod { static int a=10; void display(){ System.out.println("This is an Instance method"); } static void show(){ System.out.println("This is a Static method"); } public static void main(String[] args) { StaticMethod sm=new StaticMethod(); sm.display(); StaticMethod s=null; s.show(); int c=s.a; System.out.println(c); } }
    Output: This is an Instance method This is a Static method 10
➲ Static method can access a static variable and can change the value of it.
Program source code 3: In this example, we will change the value of the static variable inside the static method.
    package com.scientecheasy; public class ValueChange { static int a=10; static int change(){ // Change the value of static variable. int a=20; return a; } public static void main(String[] args) { // Call the S.M. using the class name. Since it will return an integer value. So we will store it by using a changeValue variable. int changeValue=ValueChange.change(); System.out.println(changeValue); } } Output: 20
Program source code 4: In this program, we will calculate the square and cube of a given number by using the static method.
    package com.scientecheasy; public class SquareAndCube { static int x=15; static int y=20; static int square(int x){ // local variable. int a=x*x; return a; } static int cube(int y){ // Local variable. int b=y*y*y; return b; } public static void main(String[] args) { int sq=square(5); int cb=cube(10); System.out.println(sq); System.out.println(cb); } }
    Output: 25 1000

Can we use this or super keyword in a Static method in Java?


In entire core java, this and super keyword is not allowed inside the static method or area.
Program source code 5:
    package com.scientecheasy; public class ThisTest { // Declare the Instance variables. int x=10; int y=20; // Declare S.M. with two parameters x and y with data type integer. static void add(int x, int y){ System.out.println(this.x+this.y); // Compile time error. } public static void main(String[] args) { ThisTest.add(20, 30); } }
    Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Cannot use this in a static context Cannot use this in a static context

How to call a static method in Java from another class?


We can call a static method in Java from another class directly using the class name. Let's see an example program to understand the concept.
Program source code 6: In this example, we will create a class Student and declare the static methods name, rollNo, and std with the return type. 
    package com.scientecheasy; public class Student { static String name(String n){ return n; } static int rollNo(int r){ return r; } static int std(int s){ return s; } }
Now create another class StudentTest and call the static method with passing argument values.
    package com.scientecheasy; public class StudentTest { public static void main(String[] args) { // Call S.M. using class name and pass the String argument. Since it will return String value. so we will store the value by nameStudent and print the output on the console. String nameStudent=Student.name("Shubh"); // Call and pass the integer value. Since it will return an integer value. So we will store the int value by rollStudent and std. int rollStudent=Student.rollNo(5); int std=Student.std(8); System.out.println("Name of Student: " +nameStudent); System.out.println("Roll no. of Student: " +rollStudent); System.out.println("Standard: " +std); } }
    Output: Name of Student: Shubh Roll no. of Student: 5 Standard: 8
Program source code 7: Factorial series using a static method.
    package com.scientecheasy; public class Factorial { static int f=1; static void fact(int n){ for(int i=n;i>=1;i--){ f=f*i; } } }
    package com.scientecheasy; public class FactorialTest { public static void main(String[] args) { Factorial.fact(4); System.out.println(Factorial.f); } }
    Output: 24

Difference between Static method and Instance method


1. A static method is also known as a class method whereas Instance method is also known as non-static method.
2. The only static variable can be accessed inside the static method whereas, Static and instance variable both can be accessed inside the instance method.
3. We do not need to create the object of the class for accessing the static method whereas, in the case of an instance method, we need to create the object for access.
4. It cannot be overridden whereas, an Instance method can be overridden.
5. Memory is allocated only once at the time of class loading whereas, In the case of the instance method, Memory is allocated multiple times whenever the method is calling.

Always Remember:
1. Static (variable, method, and inner class) are stored in Permanent generation memory(class memory).
2. It can be overloaded but cannot be overridden because they are bound with class, not instance.

Final words:
We hope that this article will help you to understand the concepts of the static method with example and programs. We have covered almost all the important topics related to it. 

BEST WISHES TO YOU AND YOUR FAMILY THIS GANESH CHATURTHI!
                                         HAPPY GANESH CHATURTHI

Hot Post

Java Collections Framework | Need & Advantages