user979587
user979587

Reputation: 263

Calling another method into my class

I would like to implement a method that I use in another class in my project package.

The class that I would like to add the method to does not extend the class where the method comes from.

I've tried:

MyMethod p = new MyMethod;

When I do this I get 'cannot resolve symbol 'MyMethod'

Upvotes: 0

Views: 324

Answers (2)

vikas27
vikas27

Reputation: 573

May be the method is private . U cannot call private method in other class.

This is simple:

Class A{

  public void methosAA(){

  }

}


Class B{

A a=new A();

 public static void main(){
    a.methosAA();
}

}

Upvotes: 0

Swagatika
Swagatika

Reputation: 3436

The statement MyMethod p = new MyMethod

is syntactically incorrect. If MyMethod is a class , and if you want to create an instance of it to call any method of it :

Then the correct syntax to instantiate would be :

  MyMethod p = new MyMethod(); 

Then you need to implement methods and call it with the newly created instance p.

If you are asking about calling a method from different class existing in a different package, you 1st have to import that class in your MyMethod class, then have to create an instance of that class, or cast with that class to be able to call the method.

Upvotes: 2

Related Questions