Java Program For Static Keyword Execution

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

//static variable
package testcoreproject;

public class Abc 
{
     static int count=0;
     Abc()
     {
         count++;
     }        
   void display() 
   {
       System.out.println("Total Object Created: "+count);
   }
}

Program 2

package testcoreproject;


public class Abc1 
{
     int m=10;
     static int n=10;
     void incr()   // Non static 
     {
         ++m;
         ++n;
     }
     static void display() //Static 
     {
      //   System.out.println(m);
         System.out.println(n);
     }
}

Program 3

// Static method
package testcoreproject;


public class MyMath 
{
    MyMath()
    {
        System.out.println("I am constructor block");
    }
    static
    {
        System.out.println("I am static block");
    }
    static public int square(int n)    
    {
        return n*n;
    }
     static public int cube(int n)    
    {
        return n*n*n;
    } 
    static public int power(int n,int p)
    {
          int s=1;
          while(p!=0)
          {
              s=s*n;
              p--;
          }
          return s;
    }        
}

Program 4

package testcoreproject;

import static testcoreproject.Abc.count;


public class TestStatic1 
{
    public static void main(String[] args) 
    {
        MyMath M1=new MyMath();
        System.out.println(MyMath.square(5));
         //  System.out.println(MyMath.power(5,4));
//         Abc A1=new Abc();
//         Abc A2=new Abc();
//         Abc A3=new Abc();
//         Abc A4=new Abc();
//         Abc A5=new Abc();
//        System.out.println("Total Object Created: "+Abc.count);
        
    }
    
}

 

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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