I'd just started learning about passing information to methods and constructors in Java when I ran into this
public Polygon polygonFrom(Point[] corners){...
How is the Polygon object created in the method declaration? Is it the return type and how?
Asked
Active
Viewed 52 times
-1

Ten Kei
- 3
- 2
-
1That is a regular method that returns a `Polygon` instance. You can probably take a look at the source code somewhere. – f1sh Mar 24 '21 at 10:26
-
That depends on what the Polygon object constructor is. If it takes and array of Points then `return new Polygon(corners);` – Tarmo Mar 24 '21 at 10:27
1 Answers
0
Basically methods have to following structure
[visibility] [return type] [name] ([argument1 type] [argument1 name], ...) {
[method body]
}
for your example this means: Yes, Polygon is the return type of your method "polygonFrom". How this object is created is completely up to you. You could just call
return new Polygon()
(maybe with constructor parameters).
But in general that is exactly the purpose of a method to create this Polygon anyhow to not repeat this everytime you need to access a Polygon this way.

Wai Ha Lee
- 8,598
- 83
- 57
- 92

Michael
- 81
- 2