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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:28:53+00:00 2026-05-23T06:28:53+00:00

I am not new to java and C#. I thought I understand the concept

  • 0

I am not new to java and C#. I thought I understand the concept of variable scope until recently I was asked this question in an interview:

public class Q{ //starting y scope

    static int x = 11;
    private int y = 33; // Just added a “private” modifier to make it clearer.

    public static void main(String args[]){
        Q q = new Q();
        q.call(5);
    }

    public void call(int x){
        Q q = new Q();

        this.x = 22;
        y = 44;

        System.out.println("Output: " + Q.x);
        System.out.println("Output: " + q.x);
        System.out.println("Output: " + q.y);
    }

} //ending y scope

Define the output of this program.

I answered the question during the interview that the output would be a runtime exception. To my understanding, y is declared private, and the instance method call() is trying to access the instance private variable y of another instance of class Q. How could that happen at all!? However answering this question wrongly didn’t affect my interview too much because this is the kind of “tricky basic” question. But, answering wrongly means my years’ Java experience needs rehab, that’s terrible!

Could someone help me on this matter? I would be so much appreciated!

  • 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-23T06:28:54+00:00Added an answer on May 23, 2026 at 6:28 am

    You can access private members of your own class, even from a different instance of the class.

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

Sidebar

Related Questions

How come, in Java, I can write List<?> list = new LinkedList<Double>(); but not
This is not a new topic, but I am curious how everyone is handling
I'm not brand new to the concept of unit testing but at the same
its a maybe a dumb question but i am curious to understand this thing....
As a C# developer new to Java, i thought it might be easiest if
I'm not new to web publishing, BUT I am new to publishing against a
I'm new to Flex, although not new to programming. I want to write a
I'm really new to ASP.Net Mvc, but not new to Asp.Net. I was aware
Are there any good books for a relatively new but not totally new *nix
Being new to Cocoa, and probably not knowing all of the potential classes available

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.