1

I'm currently working on this school project and I can't figure out how to call this method mainly because I don't even know what to search for. However I do believe that I am suppose to create an object and reference that? I'm also calling from another method and not my main.

    public String[] unique(String[] words) {
        //more code
    return resizeStringArray(uniqueWords,nUnique);
}

2 Answers2

2

There are two ways you could achieve.

  1. create an object/instance and call the method
  2. declare method as static and call with Class name

Let's see here Approach 1

Class SomeClass {
 public String[] unique(String[] words) {
        //more code
    return resizeStringArray(uniqueWords,nUnique);
}
 public static void main(String[] args) {
     SomeClass classInstance = new SomeClass();
     //calling method
     classInstance.unique(words);
 }
}

Approach 2

Class SomeClass {
 public static String[] unique(String[] words) {
        //more code
    return resizeStringArray(uniqueWords,nUnique);
}
 public static void main(String[] args) {
     // Use only Classname and "."
     SomeClass.unique(words);
 }
}
ASP
  • 437
  • 4
  • 11
0

So you must have the method in a class declaration, and you say it's not in the same class as your main method, so lets call it AnotherClass. Then your method looks like this:

public class AnotherClass(){
  //initialization stuff here

  public String[] unique(String[] words) {
      //more code
  return resizeStringArray(uniqueWords,nUnique);
  }
}

then, somewhere you have instantiated an object of type AnotherClass, so you can call it with dot notation, something like

AnotherClass myinstance = new AnotherClass;

and then

someUniqueWords[] = myinstance.unique(String[] somewords)

If you don't have an instantiation then you can just call the method via

someUniqueWords[] = unique(String[] somewords)
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51