Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to access methods from another class

Tags:

java

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?

like image 386
co757 Avatar asked Jul 04 '11 23:07

co757


People also ask

How do you call a method from another class file?

If you want to acess it from another class file then you have to instantiate an Object and then access it using the public method: Main m = new Main(); int a = m. getVariableName(); Hope it helps.

How do you call one method from another method in Java?

Example: public class CallingMethodsInSameClass { // Method definition performing a Call to another Method public static void main(String[] args) { Method1(); // Method being called. Method2(); // Method being called. } // Method definition to call in another Method public static void Method1() { System. out.


2 Answers

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!

like image 75
krasnerocalypse Avatar answered Sep 22 '22 19:09

krasnerocalypse


Method 1:

If the method DoSomethingBeta was static you need only call:

Beta.DoSomethingBeta(); 

Method 2:

If Alpha extends from Beta you could call DoSomethingBeta() directly.

public class Alpha extends Beta{      public void DoSomethingAlpha() {           DoSomethingBeta();  //?      } } 

Method 3:

Alternatively you need to have access to an instance of Beta to call the methods from it.

public class Alpha {      public void DoSomethingAlpha() {           Beta cbeta = new Beta();           cbeta.DoSomethingBeta();  //?      } } 

Incidentally is this homework?

like image 27
Mark McLaren Avatar answered Sep 20 '22 19:09

Mark McLaren