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 } 
3
  • 2
    ClassName.methodName, though "2" is not a valid method name. Commented Feb 23, 2013 at 2:41
  • To understand what are all the valid characters allowed for Java method name. Check out here or here. Commented Feb 23, 2013 at 2:47
  • What is the relation of this question with Android, it is general. Commented Feb 23, 2013 at 3:19

2 Answers 2

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(); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

2 was basically just what I put there to try to make it easier to understand, but yeah, that worked, thanks a lot!
Great. Hopefully you're all set now.
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 } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.