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 6628455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:59+00:00 2026-05-25T22:07:59+00:00

I understood that this is a reference to the currently executing object. If that

  • 0

I understood that this is a reference to the currently executing object. If that is the case can you explain the behaviour of the following code?

public class Inherit {

    public static class ClassA
    {
        private String privateInstanceVar = "Private";
        public void classAMethod()
        {
            System.out.println("In class: " + this.getClass().getName());
            System.out.println("Can access: " + this.privateInstanceVar);
        }
    }

    public static class ClassB extends ClassA
    {
    }

    public static void main(String[] args)
    {
        ClassB b = new ClassB();
        b.classAMethod();
        //Outputs:
        //In class: Inherit$ClassB
        //Can access: Private

        //System.out.println(b.privateInstanceVar); // Fails to compile
    }   
}

The first line of classAMethod reports that this is a reference to ClassB. However, on the next line I use this to access the private instance variable privateInstanceVar of ClassA, which I shouldn’t have access to. (The commented out last line of main shows that this is indeed the case.)

So, my questions are:

  1. If this really is a reference to ClassB, how can I access the private instance variable on ClassA from a method that belongs to ClassB?
  2. Is classAMethod actually a member of ClassA and ClassB at the point of execution?
  3. If the answer to 2. is yes, what are the rules for determining in which context any given line in the method will execute?
  4. If the answer to 2. is no, then what alternative explanation is there for the behaviour of the code?
  5. Is there some bigger picture or subtlety here that I’m failing to appreciate?
  • 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-25T22:07:59+00:00Added an answer on May 25, 2026 at 10:07 pm

    If this really is a reference to ClassB, how can I access the private
    instance variable on ClassA from a method that belongs to ClassB?

    Because you inherit the method from classA, and that method accesses the private variable.

    Is classAMethod actually a member of ClassA and ClassB at the point of
    execution?

    Yes

    If the answer to 2. is yes, what are the rules for determining in
    which context any given line in the method will execute?

    Compile-time context: the method or field that is visible from the code you are writing will be chosen. Example:

    public static class ClassA{
        private String foo = "bar";
        public String getFoo(){return foo;}
    }
    public static class ClassB extends ClassA{
        private String foo = "phleem";
    }
    

    new ClassB().getFoo() will return “bar”, not “phleem”, because ClassA does not know about ClassB.

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

Sidebar

Related Questions

I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
I understood that this Bind attribute was not necessary, but JSON type is not
After I thought that I've understood how they work, I tried this: NSString *str1
With reference to this programming game I am currently building. When the game is
I am currently trying to call a method from a utility class that will
I'm programming a spider that should have realistic movement. Currently I'm using this site
I currently have a method within my class that has to call other methods,
I understand that this question could be answered with a simple sentence and that
I'll start of by saying that I understand that this topic is complicated and
I am very new to Silverlight development. I understand that this is client side

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.