Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Constructors

Aditya Puri
Aditya Puri
1,080 Points

What is a 'caller'?

At 0:16 craig says "A constructor is a method that you will run when you instantiate the class, before the object is returned to the caller".

I don't quite understand the second part of the sentence. What is a "caller". And how does he object "return to it?"

1 Answer

He did a Systems.out.println("some text" + getCharakterName("Yoda")); So lets see what happend when he used the method getCharakterName("Yoda");

public class PezDispenser {
        // first the made an instance of PezDispenser;
        // Pezdispenser dispenser = new PezDispenser("Yoda";
        // this instance has the string mCharakterName
    private String mCharakterName;

        // Here the "Yoda" of the instance of Pezdispenser ( the dispenser)
        // gets assigned to mCharakterName
    public PezDispenser(String charakterName) {
        mCharakterName = charakterName;
    }
    // and here is the method which returns the mCharakterName
    public String getCharakterName() {
        return mCharakterName;
    }
}

So he made

PezDispenser dispenser = new PezDispenser("Yoda");
// to assign a value to mCharakterName
System.out.println("The dispenser charakter  is %s\n", dispenser.getCharakterName());
// and here he wanted to get the charakters name with the method getCharakterName of the PezD instance dispenser
// this method is the caller of the private String mCharakterName. Which got it's value with the creation of the instance
Aditya Puri
Aditya Puri
1,080 Points

ok so I get what the caller is but how is the object "returned" to the caller?

Methods who are not viod (you will learn that later) have a return statement. For example the value of a lot of calculation or other operations. At the end of this methods there is always "return varablename" or "return methodName(varaible, varaible)" You dont have to understand what methods are now, you will learn that soon.

console.readLine is a build-in function of java from the class console. It's job is to return the last line of the console. This value can be stored in one of you varaibles. In this case only the String of the charakter name (mCharakterName) gets returned to the "caller"

Aditya Puri
Aditya Puri
1,080 Points

Can you help me with this question real quick, I need to understand it first before understanding this..

https://teamtreehouse.com/community/how-will-the-constructor-return-the-class