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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:45:32+00:00 2026-05-23T14:45:32+00:00

I tried to simplify my predicament as much as possible. I have three classes:

  • 0

I tried to simplify my predicament as much as possible. I have three classes:

Alpha:

public class Alpha {
     public void DoSomethingAlpha() {
          cbeta.DoSomethingBeta()  //?
     }
}

Beta:

public class Beta {
     public void DoSomethingBeta() {
          // Something
     }
}  

Main:

public class MainApp {
     public static void main(String[] args) {           
          Alpha cAlpha = new Alpha();   
          Beta cBeta = new Beta();
     }
}

I hope I did not over simplify it. My question is how do I access cBeta.DoSomethingBeta() from a method in Alpha?

  • 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-23T14:45:32+00:00Added an answer on May 23, 2026 at 2:45 pm

    You need to somehow give class Alpha a reference to cBeta. There are three ways of doing this.

    1) Give Alphas a Beta in the constructor. In class Alpha write:

    public class Alpha {
       private Beta beta;
       public Alpha(Beta beta) {
         this.beta = beta; 
       }
    

    and call cAlpha = new Alpha(cBeta) from main()

    2) give Alphas a mutator that gives them a beta. In class Alpha write:

    public class Alpha {
       private Beta beta;
       public void setBeta (Beta newBeta) {
         this.beta = beta;
       }
    

    and call cAlpha = new Alpha(); cAlpha.setBeta(beta); from main(), or

    3) have a beta as an argument to doSomethingAlpha. in class Alpha write:

    public void DoSomethingAlpha(Beta cBeta) {
          cbeta.DoSomethingBeta()
    }
    

    Which strategy you use depends on a few things. If you want every single Alpha to have a Beta, use number 1. If you want only some Alphas to have a Beta, but you want them to hold onto their Betas indefinitely, use number 2. If you want Alphas to deal with Betas only while you’re calling doSomethingAlpha, use number 3. Variable scope is complicated at first, but it gets easier when you get the hang of it. Let me know if you have any more questions!

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

Sidebar

Related Questions

I have a scenario in a system which I've tried to simplify as best
I have tried to simplify and annotate the code which is giving me a
Lets look at the following structure first (I have tried to simplify it to
I've recently tried to simplify some of my code by using generics where possible.
I've tried to simplify for the purposes of asking this question. Hopefully, this will
I apologize in advance; this is a long question. I've tried to simplify as
Note: This question has broadened in scope from previous revisions. I have tried to
I have written a polynomial class along the lines described in SICP 2.5.3 (except
I tried to declare a memory pool in my class. But the compiler shows
(Sorry for the confusion. Previous $ sign occurred when I tried to simplify the

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.