In this tutorial, we will learn about the Static Data in Java, or its types, and how to use them with the help of examples.

Static data is the data declare using ‘static’ keyword. The static data creates a single copy for the class which will be share by all the objects of that class is know as Static Data in Java.

Syntax

static datatype var;
Syntax of Static data

Example of Static data

Input Value

public class staticm {
    int n;
    static int rec;
    public void input (int z)
    {
        n=z;
        rec++;
    }
    public void show()
    {
        System.out.println("Data:" +n);
    }
    public void showrec()
    {
        System.out.println("Data:" +rec); 
    }
    public static void main (String args[])
    {
        staticm a=new staticm();
        staticm b= new staticm();
        a.input(15);
        b.input(16);
        a.show();
        a.showrec();
        b.show();
        b.showrec();
    }
}
Example of Static data in Java

Let’s run the code

Input Value

Output Value

Output Value

Static Method in Java 

This method is the method declare using static keyword. Static method define within class occupy single copy of memory which object name (but object calling also allowed) static method only holds static data.

Syntax

public static return function(par,par,…)   {
} 
Syntax of Static Method in Java 

Example of Static Method in Java 

Input Value

public class staticm {
    int n;
    static int rec;
    public void input (int z)
    {
        n=z;
        rec++;
    }
    public void show()
    {
        System.out.println("Data:" +n);
    }
    public void showrec()
    {
        System.out.println("Data:" +rec); 
    }
    public static void main (String args[])
    {
        staticm a=new staticm();
        staticm b= new staticm();
        a.input(11);
        a.show();
        b.input(12);
        b.show();
        b.showrec();
    }
}
Example of Static Method in Java 

Let’s run the code

Input Value

Output Value

Output Value

Nesting Of Methods

Nesting of methods means one user defined function can call the other user defined function without creating object of the class.

Syntax of Nesting of methods

class Main
{
    method1(){ 

    } 
    
    method2()
    {
       
       method1();
    }
    method3()
    {
      
      method2();
    }
}
Syntax of Nesting of methods

Example of Nesting of methods

Input Value

public class staticm {
    
    public void show()
   
    {
        System.out.println("Basic Engineer");
    }
    public void disp()
    {
        show();
        System.out.println("Hello Student"); 
    }
    public static void main (String args[])
    {
        staticm p= new staticm();
        p.show();
        p.disp();
    }
}
Example of Nesting of methods

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *