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?
This question is related to
java
public class WeatherResponse {
private int cod;
private String base;
private Weather main;
public int getCod(){
return this.cod;
}
public void setCod(int cod){
this.cod = cod;
}
public String getBase(){
return base;
}
public void setBase(String base){
this.base = base;
}
public Weather getWeather() {
return main;
}
// default constructor, getters and setters
}
public class Weather {
private int id;
private String main;
private String description;
public String getMain(){
return main;
}
public void setMain(String main){
this.main = main;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
// default constructor, getters and setters
}
// accessing methods
// success!
Log.i("App", weatherResponse.getBase());
Log.i("App", weatherResponse.getWeather().getMain());
Log.i("App", weatherResponse.getWeather().getDescription());
I have another solution. If Alpha and Beta are your only extra class then why not make a static variable with the image of the class.
Like in Alpha class :
public class Alpha{
public static Alpha alpha;
public Alpha(){
this.alpha = this;
}
Now you you can call the function in Beta class by just using these lines :
new Alpha();
Alpha.alpha.DoSomethingAlpha();
You either need to create an object of type Beta in the Alpha class or its method
Like you do here in the Main Beta cBeta = new Beta();
If you want to use the variable you create in your Main then you have to parse it to cAlpha as a parameter by making the Alpha constructor look like
public class Alpha
{
Beta localInstance;
public Alpha(Beta _beta)
{
localInstance = _beta;
}
public void DoSomethingAlpha()
{
localInstance.DoSomethingAlpha();
}
}
Maybe you need some dependency injection
public class Alpha {
private Beta cbeta;
public Alpha(Beta beta) {
this.cbeta = beta;
}
public void DoSomethingAlpha() {
this.cbeta.DoSomethingBeta();
}
}
and then
Alpha cAlpha = new Alpha(new Beta());
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?
Source: Stackoverflow.com