Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a class, constructor, or method? [duplicate]

Tags:

java

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
}
like image 519
J__ Avatar asked Jun 24 '26 11:06

J__


1 Answers

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

like image 148
Kick Buttowski Avatar answered Jun 26 '26 00:06

Kick Buttowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!