Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:43:38+00:00 2026-06-10T23:43:38+00:00

I was just playing around with static and inheritance to see what can and

  • 0

I was just playing around with static and inheritance to see what can and cannot be done. I have read before, that static methods cannot be overwritten, they can simply be hidden. That is why I tried to see what applies to static members. Here is some simple Test code:

public class ParentClass {
    public static String x;
    { x = "In ParentClass"; }
}

public class ChildClass extends ParentClass {
    { x = "In ChildClass"; }
}

Now when I printed x called from objects, everything went as expected:

ParentClass parentClassReference = new ParentClass();
System.out.println(parentClassReference.x); // prints: "In ParentClass"

parentClassReference = new ChildClass();
System.out.println(parentClassReference.x); // prints: "In ChildClass"

Even the trivial call to x from a child class object:

ChildClass childClassReference = new ChildClass();
System.out.println(childClassReference.x); // prints: "In ChildClass"

Now, when I call the static member, like it is supposed to be done, it gets weird:

System.out.println(ChildClass.x); // prints: "In ChildClass" (Ok, so far.)
System.out.println(ParentClass.x); // prints: "In ChildClass" (This is unexpected.)

I have no explanation for this behaviour. In the context of Overriding vs. Hiding (Code Ranch) It seemed more likely that the calls through objects shouldn’t work as expected, but the calls through class names should.

I am confused. Who can help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:43:39+00:00Added an answer on June 10, 2026 at 11:43 pm

    Your code is very strange… but the results are exactly as I’d expect.

    You need to understand three things:

    • There’s only one x variable here. Whether you reference it as ChildClass.x or ParentClass.x, it’s the same variable
    • When you refer to a static member via a reference, the compiler just translates it to a static reference, without looking at the value of the reference at all. So childClassReference.x is equivalent to ChildClass.x, which is actually compiled as ParentClass.x. Good IDEs will warn you of this sort of thing – it produced confusing code.
    • The code you’ve written which sets the value of x is only executed based on a constructor call. Basically it’s equivalent to:

      public class ParentClass {
          public static String x;
          public ParentClass() {
              x = "In ParentClass";
          }
      }
      
      public class ChildClass extends ParentClass {
          public ChildClass() {
              x = "In ChildClass";
          }
      }
      

    Fundamentally, you shouldn’t be trying to achieve any kind of polymorphism through static members. It doesn’t work, and attempts to fake it will not end happily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just playing around with WPF and MVVM, and I have made a simple
I'm just playing around with FastMember and have hit a problem. Each time I
Just playing around with interfaces and I have a question about something which I
Just playing around with some of the APIs in .NET and I can't seem
I have started playing around with Geolocation, and I can get the co-ordinates etc.
I was just playing around with the id() function & noticed that if you
I am just playing around with Go and do not yet have a good
So, I was just playing around making some page for fun. I cannot seem
I have one last problem that is killing me. I am playing around with
I was just playing around with Rails 3 beta and noticed that link_to_function is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.