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

  • SEARCH
  • Home
  • 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 500249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:04:05+00:00 2026-05-13T06:04:05+00:00

While studying for the SCJP 6 exam, I ran into this question in a

  • 0

While studying for the SCJP 6 exam, I ran into this question in a test exam:

class A{  
    private static String staticProperty = " a ";  
    String getStaticProperty() { return staticProperty; }  
}  

class B extends A {  
    private static String staticProperty = " b ";  
    public static void main(String[] args){  
        new B().go(new A());  

    }  
    void go(A t){  
        String s = t.getStaticProperty() + B.staticProperty + staticProperty + (new B().getStaticProperty());  
        System.out.println(s);  
    }  
}  

What’s the output??

The output here is a b b a

I perfectly understand a b b, but don’t understand the “a” at the end. If you inherit a method (in this case, B inherits getStaticProperty() from A), and that method returns a static variable from the parent (staticProperty), which you re-define in the child, you will ALWAYS use the parent static variable value??

By the way, removing the static identifier and making staticField an instance member of the classes returns the same results. Modifying access modifiers from private to public or other returns the same results. I needed to override the getStaticProperty method in order to get what I wanted to see.

  • 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-05-13T06:04:05+00:00Added an answer on May 13, 2026 at 6:04 am

    Field access is not subject to dynamic dispatch like method access, i.e. fields cannot be overriden. This line in class A:

    String getStaticProperty() { return staticProperty; }  
    

    therefore refers to the field staticProperty in class A. Class B’s field with the same name is irrelevant, or rather: it hides the superclass field, and all code in class B will use this field. Sun’s Java tutorial has this to say on the matter:

    Within a class, a field that has the
    same name as a field in the superclass
    hides the superclass’s field, even if
    their types are different. Within the
    subclass, the field in the superclass
    cannot be referenced by its simple
    name. Instead, the field must be
    accessed through super, which is
    covered in the next section. Generally
    speaking, we don’t recommend hiding
    fields as it makes code difficult to
    read.

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

Sidebar

Related Questions

I came across this question while studying for the Microsoft Web-Application Developer exam, You
While studying for a Functional Programming exam, I came across the following question from
While I am studying the new django docs on class-based views, I notice this
While studying for a Functional Programming exam, I came across the following questions from
While studying for the Zend PHP Exam I came across the following contradicting information:
I just saw this code while studying the wordpress source code (PHP), You can
I am studying the SCJP, and while studying I have found an exercise that
While studying for the 70-433 exam I noticed you can create a covering index
I've been studying C programming for a while and I've stumbled on this exercise
While studying a software methodologies, I often see disclaimers along the lines of This

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.