4 Replies - 236 Views - Last Post: 17 June 2021 - 02:01 AM Rate Topic: -----

#1 a36   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 23-February 20

how to implement a public List<String> something(int n) {} met

Posted 16 June 2021 - 04:10 PM

Inside of a Java class in the simplest program that just has a public static void main(String args[]) entry point inside of the same class, I'm presented with one other method:

public List<String> task(int n) {
  // implement something here
}


Can somebody please show me an example of how to call that task method from a statement elsewhere? I've tried everything, and have spent hours trying to find samples online where somebody calls that and furnishes an argument from somewhere else in the program; I can't find anything. I have a Java reference book that's two and a half inches thick and can't find a trace of an example of how to do that.

Thank you for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: how to implement a public List<String> something(int n) {} met

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,679
  • Joined: 25-December 13

Re: how to implement a public List<String> something(int n) {} met

Posted 16 June 2021 - 04:21 PM

The way to call a non-static method is to provide a reference to the class, a dot and the method name ( args )
 List<String> list = refToClass.task(1123);

Was This Post Helpful? 0
  • +
  • -

#3 a36   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 23-February 20

Re: how to implement a public List<String> something(int n) {} met

Posted 16 June 2021 - 07:22 PM

View PostNormR, on 16 June 2021 - 04:21 PM, said:

The way to call a non-static method is to provide a reference to the class, a dot and the method name ( args )
 List<String> list = refToClass.task(1123);


import java.util.List;

public class Flow {
  public List<String> task(int n) {
    // implement something here
  }

  public static void main(String args[]) {
    Flow f = new Flow();
    List<String> list = f.task(1123);
  }
}



Thank you for responding. I don't know how to compose this reference creation. It fails and the error I get is...
symbol: method task(int)
location: variable f of type Flow
Was This Post Helpful? 0
  • +
  • -

#4 a36   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 23-February 20

Re: how to implement a public List<String> something(int n) {} met

Posted 16 June 2021 - 09:33 PM

I think I have something working now. It's a single class, with the main() entry point. I just tried List<String> list = task(1123); and provided a suitable return inside the task method, and it compiled. Thank you again.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3737
  • View blog
  • Posts: 17,104
  • Joined: 20-September 08

Re: how to implement a public List<String> something(int n) {} met

Posted 17 June 2021 - 02:01 AM

Quote

Thank you for responding. I don't know how to compose this reference creation. It fails and the error I get is...


I'm guessing that's because you tried to run that without being able to compile it, which for some reason unknown to me, some IDEs will allow. Your code would not compile as the method task has no return value
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1