Static methods cannot be overridden but can be redefined in child Class

class Animal 
{
  static void doStuff() 
  {
    System.out.print("animal");
  }
}

class Dog extends Animal 
{
   // it's a redefinition
   // not an override
   static void doStuff() 
   { 					
     System.out.print("dog");
   }

   public static void main(String [] args) 
   {
      Animal [] a = {new Animal(), new Dog(), new Animal()};
     
      for(int x = 0; x < a.length; x++)
     	a[x].doStuff();               // invoke the static method 

    }
}

Output

animal animal animal

Method overriding is made possible by dynamic dispatching, meaning that the declared type of an object doesn’t determine its behavior, but rather its runtime type

Animal lassie = new Dog();
lassie.speak(); // outputs "woof!"
Animal kermit = new Frog();
kermit.speak(); // outputs "ribbit!"

Even though both lassie and kermit are declared as objects of type Animal, their behavior (method .speak()) varies because dynamic dispatching will only bind the method call .speak() to an implementation at run time – not at compile time.

Now, here’s where the static keyword starts to make sense: the word “static” is an antonym for “dynamic”. So the reason why you can’t override static methods is because there is no dynamic dispatching on static members – because static literally means “not dynamic”. If they dispatched dynamically (and thus could be overriden) the static keyword just wouldn’t make sense anymore.

public class Why {

  public static void test() {
    System.out.println("Passed");
  }

  public static void main(String[] args) {
    Why NULL = null;
    NULL.test();
  }
}

test() is a static method. A static member belongs to the type, and do not require an instance to access.

A static member should ONLY be accessed via a type expression. That is, you should’ve written it as follows:

Why.test(); // always invoke static method on the type it belongs to!

Java does allow you to access a static member via an object reference expression, but this is VERY misleading, since this is NOT the actual semantics of a static member access.

Why aNull = null; 
aNull.test(); // DO NOT EVER DO THIS!
// invokes Why.test(), does NOT throw NullPointerException

When accessing a static member through an object reference expression, only the declared type of the reference matters. This means that:

  1. It doesn’t matter if the reference is actually null, since no instance is required
  2. If the reference is not null, it doesn’t matter what the runtime type of the object is, there is no dynamic dispatch!!!
package packA;

public class sam1 
{
 public static final double DIAMETER = 12756.32; // kilometers
 
 public static void hithere()
 {
	 System.out.println("Hi There");
 } 
}


package packB;
import packA.sam1;

public class sam2 
{
	public static void main(String args[])
	{
		sam2 objsam2 = new sam2();
		objsam2.halfway();		
	}
	
	public void halfway()
	{ 
		sam1.hithere();
		System.out.println(sam1.DIAMETER/2.0);
	}
}

-Two Packages – packA and packB – While importing static from packA to packB you should either use import packA.sam1; or import static packA.sam1.*;

Using import static packA.sam1; will not allow to access elements in package

If you use import packA.sam1;
className.StaticVariableName

If you use import static packA.sam1.*;
StaticVariableName

How to call Static Method in Another Package
Sample4.java

package com.scjp.test;

public class Sample4 
{
  public static void CallMe()
  {
   System.out.println("I am Static in Another Package");	
  }

  public static void main(String[] args) 
  {	
  }
}

Sample6.java

import com.scjp.test.*;

public class Sample6 
{
  public static void main(String[] args) 
  {
    Sample4.CallMe();
  }
}