Difficulty: Intermediate | Prerequisites: Java Identifiers, Variables and Data Types
Functions (methods) in Java let you package reusable logic with a declared return type and parameters. Java is pass-by-value, which means a method receives a copy of its arguments and cannot change the originals. Classes bundle data (fields) and behaviour (methods) into objects, with constructors to initialise them and access modifiers to control visibility.
Method (function)
A named block of code that performs a task, optionally accepts parameters, and optionally returns a value. In Java, every method must declare a return type.
Think of it as a recipe: you give it ingredients (parameters) and it gives you back a dish (return value).
Return type
The data type of the value a method sends back to the caller. Placed before the method name in the declaration. If the method returns nothing, the return type is void.
Parameter
A variable declared in a method's signature that acts as a placeholder for data passed in when the method is called.
Argument
The actual value passed to a method when it is called. The argument fills the parameter.
void
A return type indicating the method does not return any value. A void method performs an action but sends nothing back.
Pass-by-value
Java's mechanism for passing arguments to methods. The method receives a copy of the argument's value. Changes to the parameter inside the method do not affect the original variable outside it.
In simple terms, the method works on a photocopy, not the original document.
Class
A blueprint for creating objects. It defines the fields (data) and methods (behaviour) that its objects will have.
Object
An instance of a class, created using the new keyword. Each object has its own copy of the class's fields.
Constructor
A special method that initialises a new object. It has the same name as the class and no return type.
Default constructor
A constructor that takes no arguments. It sets fields to default or placeholder values.
Overloaded constructor
A constructor that takes arguments, allowing you to set field values at the time of object creation.
private
An access modifier that restricts a field or method to the class it is defined in. Other classes cannot access it directly.
public
An access modifier that allows a field or method to be accessed from any other class.
Getter
A public method that returns the value of a private field. Follows the naming pattern getFieldName().
Setter
A public method that updates the value of a private field. Follows the naming pattern setFieldName(newValue).
this keyword
Refers to the current object instance. Used inside a class to distinguish between a field and a parameter with the same name. this.year = year; assigns the parameter to the field.
The basic outline:
public static returnType name(parameterType paramName) {
// do something with your parameters
// return something of your return type
}A method that returns an int:
public static int nPlusThree(int n) {
return n + 3;
}The value of n does not change. The method creates a new value (n + 3) and returns it.
A method that returns a String:
public static String stringNPlusThree(int n) {
return "3" + n;
}This concatenates the String "3" with the integer n. The result is a String.
This is the concept the exam will try to catch you on:
public static void changeN(int n) {
n = 3; // only changes n inside this method
}
public static void main(String args[]) {
int n = 5;
changeN(n);
System.out.print(n); // prints 5, not 3
}The output is 5. The changeN method receives a copy of n. Setting it to 3 inside the method has no effect on the original n in main.
public class Car {
private int year; // private field
private String model; // private field
// Default constructor (no arguments)
public Car() {
this.year = 0;
this.model = "";
}
// Overloaded constructor (takes arguments)
public Car(int year, String model) {
this.year = year;
this.model = model;
}
// Getter
public String getModel() {
return this.model;
}
// Setter
public void setModel(String newModel) {
this.model = newModel;
}
// Getter
public int getYear() {
return this.year;
}
// Setter
public void setYear(int newYear) {
this.year = newYear;
}
}Key points:
Fields are private, so other classes cannot access them directly
Getters and setters are public, providing controlled access
The constructor name matches the class name exactly
this.year refers to the object's field; year alone refers to the parameter
Syntax: ClassName objectName = new ClassName(arguments);
Example: Car car1 = new Car(2016, "malibu");
This calls the overloaded constructor and sets year to 2016 and model to "malibu". To access or change these values from another class, use the getters and setters: car1.getModel() returns "malibu".
Students often think a method can change the value of its arguments in the calling code. It cannot. Java is pass-by-value: the method works on a copy. This is the single most tested trap in CSE 110 function questions.
Students confuse the constructor with a regular method. Constructors have no return type (not even void) and must share the class's exact name.
Students try to access private fields directly from another class (e.g., car1.year). This will not compile. Use the getter instead: car1.getYear().
Students forget that a class can have multiple constructors (overloading). The one called depends on the arguments passed.
⚠️ Pass-by-value tracing questions are a staple. If a method sets its parameter to a new value, the variable in main is unchanged. Use scratch paper to track variable values.
⚠️ Know the difference between a default constructor (no arguments) and an overloaded constructor (takes arguments).
⚠️ Understand access modifiers: private fields cannot be read or written from outside the class. Getters and setters are the only route.
⚠️ The this keyword distinguishes a field from a parameter of the same name inside a constructor or method.
True or false: A void method can contain a return statement.
Answer: True. It can use return; (with no value) to exit early, but it cannot return a value.
Fill in the blank: In Java, arguments are passed by ____.
Answer: value
True or false: A constructor must have a return type.
Answer: False. Constructors have no return type.
What keyword is used to refer to the current object's fields inside a class?
Answer: this
True or false: You can access a private field directly from a different class.
Answer: False. You must use a public getter.
Q: What does the following code print?
public static void doubleIt(int x) {
x = x * 2;
}
public static void main(String[] args) {
int num = 10;
doubleIt(num);
System.out.println(num);
}A: 10. The method doubleIt receives a copy of num. Changing x inside the method does not affect num in main.
Q: Write a default constructor for a class called Student with fields String name and int age.
A: public Student() { this.name = ""; this.age = 0; }
Q: Given a class Dog with a private field String breed and a public getter getBreed(), how do you retrieve the breed of a Dog object called myDog?
A: myDog.getBreed()
Q: What is the return type of a method declared as public static String greet(String name)?
A: String.
Q: Can a class have both a default constructor and an overloaded constructor?
A: Yes. This is called constructor overloading. Java selects the correct constructor based on the arguments provided.
This connects to Arrays because arrays can be passed to methods and returned from them. It connects to Strings because string concatenation inside methods is a common exam question. Understanding classes is foundational for any future object-oriented programming course.
Java methods, Java functions, return type, void, parameters, arguments, pass by value, pass-by-value, class, object, constructor, default constructor, overloaded constructor, private, public, getter, setter, accessor, mutator, this keyword, encapsulation, object-oriented programming, OOP, CSE 110, ASU, Arizona State, intro Java