1

I have 2 classes, MainActivity and MainGame. If I have a public static void in MainActivity, and one in MainGame. How would I execute the one in MainGame from MainActivity?

For example, I have this:

Main Activity:

public static void 1()
{
2();
}

Main Game:

public static void 2()
{
//blah blah blah
}
Govind Balaji
  • 639
  • 6
  • 17
user2101454
  • 291
  • 1
  • 4
  • 7
  • 2
    ClassName.methodName, though "2" is not a valid method name. – Hot Licks Feb 23 '13 at 02:41
  • To understand what are all the valid characters allowed for Java method name. Check out [here](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8) or [here](http://stackoverflow.com/questions/10211135/java-valid-character-for-method-name). – Jayamohan Feb 23 '13 at 02:47
  • What is the relation of this question with `Android`, it is general. – Govind Balaji Feb 23 '13 at 03:19

2 Answers2

4

2 isn't a valid method name I think, but if it were you'd just do:

MainActivity.2();

but let's say it isn't and you called it two instead, then maybe you're looking for

public class MainGame {
    public static void one() {
        System.out.println("called one()");
    }
}

public class MainActivity {

    public static void two() {
        MainGame.one();
    }

}
rainkinz
  • 10,082
  • 5
  • 45
  • 73
1

In Java All Names must start with a '_' or alphabet.

So, we can take the method names 1 as _1 and 2 as _2.

The syntax for calling static methods in other classes is ClassName.MethodName(arguments).
So, in this case you would change the code as follows:

class MainActivity{
public static void _1()
{
MainGame._2();
}
}
class MainGame{
public static void _2()
{
//blah blah blah
}
}
Govind Balaji
  • 639
  • 6
  • 17