---
title: "Example of Overrinding static method in java"  
description: "Example of Overrinding static method in java"  
author: "Anonymous User"  
published: 2014-10-10  
updated: 2014-10-10  
canonical: https://www.mindstick.com/forum/2355/example-of-overrinding-static-method-in-java  
category: "java"  
tags: ["java", "oops"]  
reading_time: 2 minutes  

---

# Example of Overrinding static method in java

i know that we [cannot override](https://www.mindstick.com/interview/2264/why-we-cannot-override-static-method) [static methods](https://www.mindstick.com/forum/161339/static-methods-in-python) in java. I read here why:

but can someone [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the following [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
    class A    {        public static void a()        {             System.out.println("A.a()");        }    }        class B extends A    {        public static void a()        {            System.out.println("B.a()");        }    }
```

how was I able to override [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) a() in [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) B? why did it work here or am I [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) something.\

## Replies

### Reply by Anonymous User

you didn't override anything here. To see for yourself, Try putting @Override annotation before public [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) void a() in class B and Java will throw an error.\
You just defined a function in class B called a(), which is distinct (no relation whatsoever) from the function a() in class A.\
But Because B.a() has the same name as a function in the parent class, it hides A.a() [As pointed by Eng. Fouad]. At runtime, the compiler uses the actual class of the declared reference to determine which method to run. For example,

```
B b = new B();       b.a();       A a = (A)b;       a.a()
```

You cannot override static [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) in Java. Remember static methods and fields are associated with the class, not with the objects. (Although, in some languages like Smalltalk, this is possible).\


---

Original Source: https://www.mindstick.com/forum/2355/example-of-overrinding-static-method-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
