Learn Java Tutorial for Beginners (Video), Part 14: Methods

How to add methods (subroutines) to your classes in Java. In this tutorial we'll look at adding subroutines to our basic Person class, so that the Person objects can do stuff as well as containing data.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.



Code for this tutorial:

 
class Person {

    // Instance variables (data or "state")
    String name;
    int age;

    // Classes can contain

    // 1. Data
    // 2. Subroutines (methods)
    
    void speak() {
        for(int i=0; i<3; i++) {
            System.out.println("My name is: " + name + " and I am " + age + " years old ");
        }
    }
    
    void sayHello() {
        System.out.println("Hello there!");
    }
}

public class App {

    public static void main(String[] args) {

        // Create a Person object using the Person class
        Person person1 = new Person();
        person1.name = "Joe Bloggs";
        person1.age = 37;
        person1.speak();
        person1.sayHello();

        // Create a second Person object
        Person person2 = new Person();
        person2.name = "Sarah Smith";
        person2.age = 20;
        person2.speak();
        person1.sayHello();

        System.out.println(person1.name);

    }

}

 
My name is: Joe Bloggs and I am 37 years old 
My name is: Joe Bloggs and I am 37 years old 
My name is: Joe Bloggs and I am 37 years old 
Hello there!
My name is: Sarah Smith and I am 20 years old 
My name is: Sarah Smith and I am 20 years old 
My name is: Sarah Smith and I am 20 years old 
Hello there!
Joe Bloggs