CSE 121
CSE 121
  • Home / Calendar
  • Syllabus
  • Assignments
  • Resubmissions
  • Exam
  • Getting Help
  • Course Staff
  • Grading Rubrics
  • Resources

  • Course Tools
  • EdStem
  • Grade Calculator
  • Anonymous Feedback
  • Acknowledgements

Attention!

This website is still under development. More information will be added soon and all content is subject to change.

Methods and Parameters


Methods¶

(a group of statements that runs when called, also reduces redundancy)

Declaration¶

Syntax

public static void name() {
    statements;
}

Example

public static void printHello() {
    System.out.println("Hello World");
    System.out.println("Have a great day!");
    System.out.println();
}

Call¶

Syntax

methodName();

Example

public static void main(String[] args) {
    printHello();
    printHello();
}

Output

Hello World
Have a great day!

Hello World
Have a great day!

Class Constant¶

(unchangeable global values that can be seen within your class)

Declaration¶

Syntax

public class ClassName() {
    ...
    public static final <type> <name> = <value>;
}

Example

public class Hello() {
    ...
    public static final int DAYS_PER_WEEK = 7;
}

Parameters¶

(a way to pass information into a method)

Declaration¶

Syntax

public static void name(type name, ...) {
    statements;
}

Example

public static void box(int width, int height) {
    for (int i = 1; i <= height; i++) {
        for (int j = 1; j <= width; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}

Call¶

Syntax

methodName(value, ..., value);

Example

public static void main(String[] args) {
    box(10, 7); // width = 10, height = 7
    box(5, 3);  // width = 5, height = 3
}

Output

**********
**********
**********
**********
**********
**********
**********
*****
*****
*****

Search

Search the class website; related sections and pages will appear below. Note: this search is not as forgiving with typos as other search engines.