Java for Complete Beginners (Video), Part 15: Getters and Return Values

Using get methods or “getters” in your Java classes, and how to return values from methods in general.

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 {
	String name;
	int age;
	
	void speak() {
		System.out.println("My name is: " + name);
	}
	
	int calculateYearsToRetirement() {
		int yearsLeft = 65 - age;
		
		return yearsLeft;
	}
	
	int getAge() {
		return age;
	}
	
	String getName() {
		return name;
	}
}


public class App {

	public static void main(String[] args) {
		Person person1 = new Person();
		
		person1.name = "Joe";
		person1.age = 25;
		
		// person1.speak();
		
		int years = person1.calculateYearsToRetirement();
		
		System.out.println("Years till retirements " + years);
		
		int age = person1.getAge();
		String name = person1.getName();
		
		System.out.println("Name is: " + name);
		System.out.println("Age is: " + age);
	}

}

Years till retirements 40
Name is: Joe
Age is: 25


 

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

2 Responses to Java for Complete Beginners (Video), Part 15: Getters and Return Values

  1. Tim Alm says:

    I have a question. I created Person as a separate class file referenced by the tutorial class file I created. I set an int variable named years as a pointer to the calculateYearsToRetirement method that I created within When I try to reference the age to retirement I can get it to work if I reference the method directly but if i reference the pointer to the method it returns 65.
    Here’s what doesn’t work…keep in mind these are 2 separate class files.

    class Person
    {
    String name;
    int age;
    int yearsLeft;

    void speak()
    {
    System.out.println(“My name is ” + name);
    }

    int calculateYearsToRetirement()
    {
    yearsLeft = 65 – age;
    return yearsLeft;
    }
    }

    public class Tutorial13
    {

    public static void main(String[] args)
    {
    Person person1 = new Person();
    Person person2 = new Person();
    int years = person1.calculateYearsToRetirement();
    int years2 = person2.calculateYearsToRetirement();

    person1.name = “Tim Alm”;
    person1.age = 46;

    person2.name = “Janice Buster”;
    person2.age = 65;

    System.out.println(person1.name + ” is ” + person1.age);
    System.out.println(“I will retire in ” + years + ” years”);
    person1.speak();
    System.out.println(person2.name + ” is ” + person2.age);
    System.out.println(“I will retire in ” + years2 + ” years”);
    }

    }

    When i change the lines that print when the people will retire to

    System.out.println(“I will retire in ” + person1.calculateYearsToRetirement() + ” years”);
    System.out.println(“I will retire in ” + person2.calculateYearsToRetirement() + ” years”);

    it works fine. Why is that?

    • Squiffy says:

      The problem is that you are calculating years to retirement before you have set the ages of the people. When you change the code, you’re now calculating the years to retirement AFTER setting their ages.

      The first code will work if you just set the age BEFORE calling calculateYearsToRetirement.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Video Tutorials (Beginners) | Tagged , , , , , |