What is the difference between a constructor and a method?
Constructors are a special type of method that is used to invoke a new object (p. 264). A constructor main purpose is to create an object. Constructors must have a similar class name and have the task of initializing the objects. Constructors are invoked when using the new operator when an object is created. A constructor differs from a method because it has no explicit return type and it is not implicitly inherited and it usually has different rules for scope modifiers.
Methods are “a collection of statements grouped together to perform an operation” (p. 178). The purpose of methods is more general than constructors. A method's basic function is to execute the Java code. Methods are reusable codes that can be used for practically any task. A method has a header that specifies a modifier such as “public static”. Method also consists of return value type, method name, and parameters of the method.
What are appropriate cases for use of a constructor? Give an example.
It is appropriate to use a constructor when providing initial values for class fields when you create new objects. Example; recording students grades received for exams throughout the semester a constructor method could be used to record all the different grades.
What are appropriate uses of a method? Give an example
If your basic function is just to execute the Java code than it’s appropriate to use a method. Example; if you had to printout each students list of grades.
Liang, Y. (2013). Introduction to Java Programming. (9th ed.) Boston, MA: PearsonConstructors are a special kind of methods that are invoked to construct objects.