Quiz 3 Java Tutorial 14: Subroutines (Methods) in Java
Overview
- This is tutorial number 14 in the beginner Java series by Cave Programming, focusing on subroutines in Java.
- Builds on the previous tutorial, which covered classes and objects; now we explore behavior (what objects can do) via methods.
- Key idea: in Java, behavior is modeled using subroutines, and a subroutine that belongs to a class is called a method.
- The tutorial uses a simple example: a Person class with data (name and age) and later adds behavior (methods).
Subroutines in Java (Methods)
- Subroutines that are part of a class are called methods.
- Terminology note: in many tutorials you’ll see both terms, but in Java the term you’ll use most often is "method" for behavior inside a class.
- A class like Person can have both data fields (attributes) and behavior (methods).
The Person class example (data and intended behavior)
- The Person class contains data attributes such as:
- name (likely a String)
- age (likely an int)
- The tutorial mentions creating objects (instances) of Person with specific names and ages, then printing information.
- The intended behavior example is for the Person to be able to express information about itself, namely its name and age, through a method (named speak in this video).
- The emphasis is on tying data (attributes) to behavior (methods) to model real-world objects.
Adding a method: defining speak()
- Plan: add a method to the Person class to expose its information via output.
- Method signature (skeleton) shown in the video:
void \, speak ( )
- The method name speak follows Java naming conventions: a method name starts with a lowercase letter, and if there are multiple words, each subsequent word starts with an uppercase letter (camelCase).
- The video notes that constructors are an exception to the naming convention, in that constructors start with an uppercase letter and must have the same name as the class; constructors will be discussed later.
- The method declaration is followed by an open curly brace, then the body, and a closing curly brace:
void speak() {
// body
}
- Practical note: when creating a method, the instructor recommends typing out the entire method signature and parentheses first, before filling in the body, to avoid forgetting the closing parentheses.
- Inside the method body, indent the code by one tab to improve readability.
- The tutorial emphasizes that proper formatting is important and will significantly improve your Java skills, not just a minor preference.
- If formatting gets messy or you forget a closing brace or parentheses, it makes the code harder to read and can lead to errors; the speaker uses an example of System.out.println to illustrate printing within the method, though the exact content is cut off in the transcript.
The System.out.println example (printing output)
- The video hints at using System.out.println to output information from the method (e.g., displaying the Person’s name and age).
- The transcript cuts off mid-example, but the intended use is to show how a method can perform actions like printing to the console.
- Concept takeaway: methods can perform operations, such as producing output, not just modify internal state.
Constructors (brief mention)
- The instructor notes one key exception to the lowercase-first-letter rule: constructors.
- Constructors have to match the class name and start with an uppercase letter, reflecting their special role in object creation.
- Constructors are not the same as regular methods and will be covered in a later lesson.
Connections to broader concepts
- Encapsulation: methods are the mechanism by which objects expose behavior while keeping internal data (name, age) potentially private.
- Modularity and reuse: defining speak() in the Person class allows all Person instances to share identical behavior without duplicating code.
- Readability and maintainability: following naming conventions and clean formatting makes code easier to read and maintain, especially as projects grow.
Practical tips for exam and coding practice
- Always start with a method skeleton:
- Choose proper return type (void for actions that don’t return a value).
- Write the method name in camelCase (lowercase first letter).
- Add the parameter list (even empty) inside parentheses.
- Open and close the braces, then fill in the body.
- Remember to indent the method body by one tab for readability.
- When you plan to add multiple methods, consider writing all method headers first and then filling in details to avoid missing braces or parentheses.
- Distinguish between methods and constructors: methods have a return type (e.g., void, int, String); constructors do not have a return type and share the class name, starting with an uppercase letter.
Possible exam-style questions to study
- What is a method in Java, and how is it related to a class?
- How do you name a method in Java, and what is camelCase?
- Why should you type the full method signature before filling in the body?
- What is the difference between a method and a constructor? How are constructors named?
- How do you use System.out.println within a method to display a Person’s name and age?
Quick recap
- Methods are the behavior of objects; they belong to classes.
- Naming conventions: methods start with lowercase, use camelCase; constructors start with uppercase and have the same name as the class.
- Skeleton: return type, method name, parentheses, braces; indent body one tab.
- Type out the signature first to avoid forgetting parentheses.
- Formatting matters for readability and maintenance.
- The transcript hints at using System.out.println to show output, though the example is cut off.