简体   繁体   中英

Java - understanding method signature

What parameters should be sent to this Java method:

public void resume(Collection<TopicPartition> partitions)

More details here .

I can see that <TopicPartition> is a Java Class with this signature:
TopicPartition(String topic, int partition)
but then after that, there is a keyword partitions in above Java method.

So is it 3 parameters or 2 or 1?

Please someone describe how should I read this kind of signatures.

Thanks.

Lets go through it step by step:

First , offcourse it is only 1 argument which is named 'partitions'

 public void resume(Collection<TopicPartition> partitions)
  • public is the access modifier, which means this method is visible from everywhere

  • void is the return type, which means there is no return value

  • resume is the methods name/identifier

Collection<TopicPartition> partitions is a litte more difficult to explain:

The Interface 'Collection'followed by a Type (TopicPartitions) means that you can input any collection of TopicPartition objects to the method. Eg:

List<TopicPartition> list = new LinkedList<>();
resume(list);  // valid, sind List or more exact LinkedList are a Collection

Queue<TopicPartition> qq = new PriorityQueue<>();
resume(qq);  // valid, sind Que or more exact PriorityQueue are a Collection

the syntax Collection<Type> is part of Java Generics, which you can have a closer looks at this tutorial .

What you call a keyword ('partitions') here is no keyword at all , but just the name/identifier of that input argument. You have to give each argument a destinct name - so you can identify it in the methods code.

in this example you can rename 'partitions' to anything you want, i would vouche for something like 'partCollection'

void, return, public, private, static, class, ... those are keywords.

Second , TopicPartition and the method you quote here is the constructor of the class TopicPartition, which needs 2 arguments: String topic and int partition and not just a method.

The constructor is the method that gets called when you instantiate a class (create a object eg using the new keyword).

So to give you a more detailed example:

List<TopicPartition> list = new LinkedList<>();
list.add(new TopicPartition("part1", 1));
list.add(new TopicPartition("part2", 2));
resume(list);  
// resume has now been called with argument of a list (which is a collection) 
// containing two TopicPartition objects whit part1, part2 and 1,2 as 
// construction arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM