How Do You Call a Method from Another Class in Android?
Contents

How can I call a method from another activity in Android?

public class MainActivity extends AppCompatActivity { // Instance of AnotherClass for future use private AnotherClass anotherClass; @Override protected void onCreate(Bundle savedInstanceState) { // Create new instance of AnotherClass and // pass instance of MainActivity by “this” anotherClass = new AnotherClass(this); …

How do you call a method from another class?

If the method is static: ClassName. methodName(); If its non static, create an object of the class first, then use the object to access the method. Use can use the method from another class by making a object by constructor and calling that object in the main method with in the same package or between many classes.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest