3

I'm having trouble identifying what exactly this is. At this point, I am familiar with what methods, constructors, and class declarations look like. Which is this? Why does it look like a constructor and a method had a baby?

public Polygon polygonFrom(Point[] corners) {  
// method body goes here
}
J__
  • 636
  • 6
  • 20

3 Answers3

5

What you have is a method

why?

In Java, method declarations have five components, in order:

  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
public Polygon polygonFrom(Point[] corners) {  
// method body goes here
}

Analyze your code snippet :

1. public is modifier

2. Polygon is return type

3. plygonForm is method name

4. (Point[] corners) is the parameter list in parenthesis

5. {} is a method body

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • 1
    Wow. That's one good answer. :) – Zizouz212 Apr 10 '15 at 02:37
  • I was under the impression that Polygon was a class, not a return type. Or can it be both? – J__ Apr 10 '15 at 02:40
  • No it is return type – Kick Buttowski Apr 10 '15 at 02:41
  • Wonderful! That was my confusion, I thought the convention was to capitalize classes, and very often the beginner's tutorial uses very basic sounding class names. Thanks! – J__ Apr 10 '15 at 02:44
  • @JasonZwick you are right about the convention but I am not sure why you are mixing two different concepts here? – Kick Buttowski Apr 10 '15 at 02:45
  • In retrospect, it does seem silly, but the way the beginner's tutorial is formatted, it can be easy for a newbie to mistake one thing for another. I could also afford to pay closer attention ^_^ – J__ Apr 10 '15 at 02:48
0

This is a method that returns type Polygon and has an array of Point as its parameter.

You can tell because you have a return type, and a method name, and a list of formal parameters in its declaration. :)

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • I was under the impression that Polygon was a class, not a return type. Or can it be both? – J__ Apr 10 '15 at 02:38
  • Use this example to understand: Polygon is a return type and can also be a class. Similar to String. String is a class, but it can also be a return type. :) – Zizouz212 Apr 10 '15 at 02:42
  • @Zizouz212 Polygon in this case is a return type that is all. – Kick Buttowski Apr 10 '15 at 02:44
0

The fact that it has a return type of Polygon identifies itself as a method, the reason why you think it looks like a constructor is because the return statment is missing in the method. The method would fail at compile time.

It should look like this:

public Polygon polygonFrom(Point[] corners) {  
// method body goes here
return null;
}
ndlu
  • 161
  • 1
  • 9
  • I'm pretty sure method body includes return statements... – Zizouz212 Apr 10 '15 at 02:43
  • Not from what the OP has provided. If the return statement is included it would not look like a constructor. Also methods with void don't have return statement. – ndlu Apr 10 '15 at 02:45
  • 1
    "Also methods with void don't have return statement." they don't need to have one, but can - i.e. read data -> `if(someCondition){return;}` -> `methodConsumingYearsOfCPUTime()` – duckstep Apr 10 '15 at 03:05