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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:42:04+00:00 2026-06-11T13:42:04+00:00

I have code like this I need to access the mysample variable of static

  • 0

I have code like this I need to access the mysample variable of static class InnerClass in the getInnerS() method which is inside the the NestedClass. I tried accessing it by creating a new object for InnerClass but i am getting java.lang.StackOverflowError.

public class NestedClass{
    private String outer = "Outer Class"; //NestedClass instance variable
    NestedClass.InnerClass innerClass = new NestedClass.InnerClass();

    void getOuterS(){
        System.out.println(outer); 
    }
    void getInnerS(){
        System.out.println(innerClass.mysample);
    }
    static class InnerClass{
        private String mysample = "Inner Class"; //InnerClass instance variable,
        NestedClass a = new NestedClass();
        void getIn(){
            System.out.println(mysample);
        }
        void getOut(){
            System.out.println(a.outer);
        }
    }
    public static void main(String[] args){
         NestedClass nestedClass = new NestedClass();
         NestedClass.InnerClass nestedInner = new NestedClass.InnerClass(); 
         nestedClass.getOuterS();
         nestedClass.getInnerS();
         nestedInner.getIn();
         nestedInner.getOut();
    }
}
  • 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-11T13:42:05+00:00Added an answer on June 11, 2026 at 1:42 pm
    NestedClass a = new NestedClass();
    

    in static InnerClass class creates an instance of the NestedClass and as InnerClass is static this is a loop.

    InnerClass does not need to be static, this should work

    public class NestedClass {
    private String outer = "Outer Class"; //NestedClass instance variable
    NestedClass.InnerClass innerClass = new NestedClass.InnerClass();
    
    void getOuterS(){
        System.out.println(outer); 
    }
    void getInnerS(){
        System.out.println(innerClass.mysample);
    }
    class InnerClass{
        private String mysample = "Inner Class"; //InnerClass instance variable,
        NestedClass a = NestedClass.this;
        void getIn(){
            System.out.println(mysample);
        }
        void getOut(){
            System.out.println(a.outer);
        }
    }
    public static void main(String[] args){
         NestedClass nestedClass = new NestedClass();
         NestedClass.InnerClass nestedInner = nestedClass.innerClass; 
         nestedClass.getOuterS();
         nestedClass.getInnerS();
         nestedInner.getIn();
         nestedInner.getOut();
    }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: class SomeObject: #code to access parents MyVar class MyClass:
I have a php code like this,and I need to convert it to .NET
If I have code with nested objects like this do I need to use
i have this code, and need help with the logic end. I would like
I have a C# code like this: using System; delegate int anto(int x); class
i have code, which does something like this: item.previous.parent.parent.aTag['href'] now i would like to
I have code like this in my view model: function ChatListViewModel(chats) { var self
I have code like this - (IBAction)onClick:(id)sender { NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:)
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have code like this: ... entry.KeyPressEvent += EntryKeyPressEvent; ... } void EntryKeyPressEvent(object o,

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.