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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:34:56+00:00 2026-06-15T10:34:56+00:00

class Host { int x=2; class Helper { int x = 7; } public

  • 0
class Host {
    int x=2;

    class Helper {
        int x = 7;
    }

    public static void main(String[] args){
        Host ho = new Host();
        Helper he = ho.new Helper();
        System.out.println(ho.x);
        System.out.println(he.x);

    }
}

So here I’m getting the expected output

2
7

Now I wanted to ask that, say, I want to access ho‘s x from he.

I.e. I want something here which will print me 2 through the Helper object he:

System.out.println(???);

I know there’s no use of such a thing, I just want to clarify my concept of nested classes.
I figure that this should be possible, because the Helper object he is sort of ‘binded’ to the Host object ho. Since he is not possible without ho. From inside the Helper class I can do System.out.println(Host.this.x); and it works. I can’t figure out how to do it from inside main.

  • 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-15T10:34:57+00:00Added an answer on June 15, 2026 at 10:34 am

    As already pointed out by other answers you can’t. The reason lies in the way this is defined in the JLS #15.8.3

    The keyword this may be used only in the body of an instance method, instance initializer, or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.

    And since you can only access the enclosing instance with this (cf JLS #15.8.4), that can only be done within the inner class:

    It is a compile-time error [ to call C.this] if the current class is not an inner class of class C or C itself.

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

Sidebar

Related Questions

public class ContactsController : ApiController { private static readonly List<Contact> _contacts = new List<Contact>();
Here's the scenario: I'd like to have a host class that can have a
I have a .h file with the following declarations: class Foo{ public: inline int
This is my class: class SSLIRC : public Bot { public: SSLIRC(void); void sockconnect(char*,
I have following appDomainManager code public class HostAppDomainManager : AppDomainManager { public override void
Below code uploads file on the FTP server public class UploadFile { static ResourceBundle
public boolean connect() { String host = talk.google.com; String port = 5222; String service
Consider the following Django models: class Host(models.Model): # This is the hostname only name
I can't understand how works the code below: class Host: name = None fileList
DECOM_CHOICES = ( ('N', 'No'), ('Y', 'Yes'), ) class Host(models.Model): hostname = models.CharField(max_length=36, unique=True)

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.