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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:10:42+00:00 2026-05-25T19:10:42+00:00

I have a class hierarchy like this: public class A { private B obj

  • 0

I have a class hierarchy like this:

public class A {

    private B obj = new B();   // Inside this object 
                               // do I need a reference to here?
                               // (In order to call method1())

    public A(){    ...    }

    private void method1(){    ...    }
    private void method2(){    ...    }
}

// Other package
public class B {
    private JButton bt1 = new JButton("Button");

    public B(){
            ...
        bt1.addMouseListener(new MouseActionsListener(this));
    }

    public class MouseActionsListener implements MouseListener
    {
        @Override
        public void mouseClicked(MouseEvent event)
        {
            /*
             * I need to call method1() HERE!!!
             */
        }
    }
}

Is it possible to call a method of the class A from the class B in that position?

The problem is that I have a list of B objects in A, and whenever a button is clicked in one of the B objects a change has to be made in A.

Thank you!

  • 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-25T19:10:43+00:00Added an answer on May 25, 2026 at 7:10 pm

    There is no implicit association that would allow you to go from B to A.

    You will need to:

    • change B to keep a reference to the corresponding instance of A;
    • initialize that reference in B‘s constructor;
    • use it call A‘s methods.

    In code:

    public class A {
    
        private B obj;
    
        public A() {
            obj = new B(this);
            ...
        }
    
        public void method1(){    ...    }
        public void method2(){    ...    }
    }
    
    
    public class B {
    
        private final A _a;
    
        public B(A a) {
            _a = a;
        }
    
        public class MouseActionsListener implements MouseListener
        {
            @Override
            public void mouseClicked(MouseEvent event) {
                _a.method1();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class hierarchy like this: @Entity @Table (name=call_distribution_policies) @Inheritance (strategy=InheritanceType.JOINED) public class
We have a class hierarchy similar to this one: public class TestDereference { private
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent
I have a hierarchy that looks something like this: class Base { public: void
I have a class hierarchy, this one: type TMatrix = class protected //... public
Here's the deal. I have a big class hierarchy and I have this one
I have a C++ class hierarchy that looks something like this: class A; class
I have a class like this public abstract class BaseType<T> { public string Name
I have a class MyClass and an inner class MyNestedClass like this: public class
Suppose I have class hierarchy like the one shown in picture. Suppose I need

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.