1

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, that way I thought the method should be allow to use since it's a public method but that was not sucesfull..

tried to use this link as reference, but I didn't understand it (since is an app for android..)

A pseudo-code to exemplify

class 1
class 2

class 2:

import package.class 1;


class 1.this.method();  -> Error: No enclosing instance of the type class 1 in is acessible in scope
Victor R. Oliveira
  • 2,434
  • 5
  • 36
  • 66
  • The link you referenced details passing data between activities, do you want to pass data or access a method? – Ljdawson Aug 06 '12 at 14:51

4 Answers4

2

Other way may be, create object for other class and then invoke the method on that object. Something like below.

new ClassA().yourMethod();
kosa
  • 63,683
  • 12
  • 118
  • 157
  • why does it needs ClassA().yourMethod(); instead of new ClassA.yourMethod(); ? it worked as you said but the second way I've tried says that classA.yourMethod can't be resolved as a type – Victor R. Oliveira Aug 06 '12 at 14:54
  • if your accessing instance method (means methods which are not static), it should be through instance of the class. So, new ClassA() is instance of ClassA. The other syntax ClassA() is not a valid syntax. Java don't know what to do with statement, so it just throws compile time error. – kosa Aug 06 '12 at 14:56
0

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().

duduamar
  • 3,628
  • 6
  • 32
  • 50
0

In java you 2 main ways to do that:

  1. Class2 extends Class1 and then you can call all protected or public methods of class 1.
  2. Get an instance of class1 inside class2:

    Class1 c1 = new Class1();
    c1.someMethod();
    
Tomer
  • 16,381
  • 13
  • 64
  • 124
0

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?

Community
  • 1
  • 1
trevor-e
  • 9,857
  • 8
  • 47
  • 73
  • unable to do that cause I use a constructor that can't be static, but thanks anyway haha wil try another time – Victor R. Oliveira Aug 06 '12 at 14:57
  • @VictorOliveira if you are trying to pass data between Activities then you should really read my link. it's hard to tell what you are doing though. – trevor-e Aug 06 '12 at 15:26
  • it's not about data, not now, but thanks for your advice, when it shows up to me this problem I will remember what you've said – Victor R. Oliveira Aug 06 '12 at 16:25