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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:07+00:00 2026-05-27T09:11:07+00:00

In most of my java applications, I have a Controller (Logic) class and a

  • 0

In most of my java applications, I have a Controller (Logic) class and a GUI class. I usually need the Logic class to call methods on the GUI class, and vice versa. Now, when the Logic constructs the GUI, I pass a reference to “this” in the constructor so the GUI can call methods on the Logic. Here is some example code.

public class Logic {
   private int num = 2;
   private final GUI gui;

   public Logic(){
       gui = new GUI(this);
   }

   public int getNum(){
      return num;
   }
}


public class GUI {
   private final Logic logic;

   public GUI(Logic logic){
      this.logic = logic;
   }

   public void calledLater(){
      int num = logic.getNum();
   }
}

My question is: Is this the best OO way to create an aggregation relationship, or am I messing up my design?

  • 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-27T09:11:08+00:00Added an answer on May 27, 2026 at 9:11 am

    Passing this to a UI class is not in itself a problem, as long as the methods of the class are not called through this, as in your example. You call getNum right in the constructor of the GUI, which should be avoided: your Logic class may not be ready to return the correct result.

    One thing to note is that you couple your GUI to the Logic class, which may be too much coupling. You should consider extracting an interface from Logic, and let GUI interact only with what’s intended for it:

    interface DataSourceForTheGui {
        int getNum();
    }
    
    class Logic implements DataSourceForTheGui {
        int getNum() {
            return n;
        }
    }
    
    class GUI {
        private readonly DataSourceForTheGui data;
        public GUI(DataSourceForTheGui data) {
            this.data = data;
        }
        public void display() {
            int k = data.getNum();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My team maintains/develops several custom .NET and Java applications. Most of these applications need
At our shop, we are maintaining roughly 20 Java EE web applications. Most of
I need to implement a browser based component (most likely java applet since it
In Java, what's generally the most accepted way to organize a class in terms
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
Most of the modern languages have built in garbage collection (GC). e.g. Java, .NET
In one of my Java 6 applications I have a thread that feeds the
I have a rather strange question: I have a Java application which uses applications
i have a few batch java command-line applications which are planned to be deployed
We have a couple of Java Web applications that serve different purposes (one for

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.