I want to call a method from one class to another, don\'t know if that\'s possible without extends the class that contains the method. I\'ve tried to import the package, tha
You need an instance of object from class 1 to invoke that method, or if it doesn't need an instance - you can declare it as static and then you can write 1.method().
If you want to call the method without creating an instance of the class then make it static
:
Class1
{
...
public static void method1() {doSomething;}
}
Class2
{
public class2()
{
Class1.method1(); //should be fine
}
}
edit: If you want to pass data between Activities
that is slightly different. Generally you do something like startActivityForResult()
and then retrieve the return value, or use Intents
like so:
How do I pass data between Activities in Android application?
In java you 2 main ways to do that:
Class2 extends Class1
and then you can call all protected
or public
methods of class 1.Get an instance of class1 inside class2:
Class1 c1 = new Class1();
c1.someMethod();
Other way may be, create object for other class and then invoke the method on that object. Something like below.
new ClassA().yourMethod();