Android Call an method from another class

后端 未结 4 573
生来不讨喜
生来不讨喜 2020-11-30 06:33

I know that this Question is repeated but I can\'t find the answer in Internet.

I want to call a method from another class.

I\'ve Class1 and Class2.

相关标签:
4条回答
  • 2020-11-30 07:09

    In Class1:

    Class2 inst = new Class2();
    inst.UpdateEmployee();
    
    0 讨论(0)
  • 2020-11-30 07:12

    And, if you don't want to instantiate Class2, declare UpdateEmployee as static and call it like this:

    Class2.UpdateEmployee();
    

    However, you'll normally want to do what @parag said.

    0 讨论(0)
  • 2020-11-30 07:21

    Add this in MainActivity.

    Intent intent = new Intent(getApplicationContext(), Heightimage.class);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-30 07:27

    You should use the following code :

    Class2 cls2 = new Class2();
    cls2.UpdateEmployee();
    

    In case you don't want to create a new instance to call the method, you can decalre the method as static and then you can just call Class2.UpdateEmployee().

    0 讨论(0)
提交回复
热议问题