Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does redefining static methods mean in Java?

Tags:

java

scjp

I've been reading a section on Statics in the SCJP study guide, and it mentions the following :

static methods can't be overridden, but they can be redefined

What does redefining actually mean? Is it a case of having a static method that exists in both parent and child, with the same signature, however they are referenced separately by their class names? Such as :

class Parent
{
   static void doSomething(String s){};
}

class Child extends Parent
{
   static void doSomething(String s){};
}

Referenced as : Parent.doSomething(); and Child.doSomething(); ?

Also, does the same apply for static variables, or just static methods?

like image 378
Jimmy Avatar asked Jul 07 '11 11:07

Jimmy


People also ask

How do we redefine a static method?

If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature (§8.4. 2) of the signature of m', in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.

Can a static method be redefined in Java?

Can we Override static methods in java? We can declare static methods with the same signature in the subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'.

What is redefining in Java?

Static method of parent class can be re-declared by child class, which hides the parent class static method. This is known as Redefinition in Java.

What is the difference between redefining and overriding a method?

Overriding facilitates class polymorphism. Overloading facilitates functional polymorphism. Redefining does neither and is an error.


2 Answers

It simply means that the functions are not virtual. As an example, say that you have an object of (runtime) type Child which is referenced from a variable of type Parent. Then if you invoke doSomething, the doSomething method of the Parent is invoked:

Parent p = new Child();
p.doSomething(); //Invokes Parent.doSomething

If the methods were non-static, doSomething of Child would override that of Parent and child.doSomething would have been invoked.

The same holds for static fields.

like image 169
Mathias Schwarz Avatar answered Oct 05 '22 17:10

Mathias Schwarz


Static means there is one per class, rather than one per object. This is true for both methods and variables.

A static field would imply that there is one such field, no matter how many objects of that class are created. Please take a look at Is there a way to override class variables in Java? for the question of overriding a static field. In short: a static field cannot be overridden.

Consider this:

public class Parent {
 static int key = 3;

 public void getKey() {
  System.out.println("I am in " + this.getClass() + " and my key is " + key);
 }
}

public class Child extends Parent {

 static int key = 33;

 public static void main(String[] args) {
  Parent x = new Parent(); 
  x.getKey();
  Child y = new Child();
  y.getKey();
  Parent z = new Child();
  z.getKey();
 }
}

I am in class tools.Parent and my key is 3
I am in class tools.Child and my key is 3
I am in class tools.Child and my key is 3

Key never comes back as 33. However, if you override getKey and add this to Child, then the results will be different.

@Override public void getKey() {
 System.out.println("I am in " + this.getClass() + " and my key is " + key);
}
I am in class tools.Parent and my key is 3
I am in class tools.Child and my key is 33
I am in class tools.Child and my key is 33

By overriding the getKey method, you are able to access the Child's static key.

like image 33
rajah9 Avatar answered Oct 05 '22 16:10

rajah9