

It’s right up there with the one about the Pilates machine and. Object: OK, you know that dream where you’re giving a talk to 500 people when you suddenly realize– you’re naked? HeadFirst: What’s the big deal about encapsulation? This week’s interview: An Object gets candid about encapsulation. If a method declares a non-void return type, it must return a value compatible with the declared return type.

A void return type means the method doesn’t return anything. (There are other things you can pass as arguments, but we’re not there yet.)Ī method must declare a return type. The value you pass as an argument to a method can be a literal value (2, ‘c’, etc.) or a variable of the declared parameter type (for example, x where x is an int variable).

Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type. The number and type of values you pass in must match the order and type of the parameters declared by the method. Methods can use instance variables so that objects of the same type can behave differently.Ī method can have parameters, which means you can pass one or more values in to the method. Things an object does are its methods (behavior). Things an object knows are its instance variables (state). In Java, you don’t have to assign or use the return value.Ĭlasses define what an object knows and what an object does. In this case, you’re calling the method for the work it does inside the method, rather than for what the method gives returns. You might want to call a method with a non-void return type, even though you don’t care about the return value. Q: Do I have to do something with the return value of a method? Can I just ignore it?Ī: Java doesn’t require you to acknowledge a return value. You must use an explicit cast when the declared type is smaller than what you’re trying to return. The caller won’t care, because the byte fits just fine into the int the caller will use for assigning the result. So, you can pass a byte where an int is expected. Q: Do I have to return the exact type I declared?Ī: You can return anything that can be implicitly promoted to that type. It’s a little more involved to return multiple values with different types we’ll be talking about that in a later chapter when we talk about ArrayList. Stuff those ints into the array, and pass it on back. if you want to return, say, three int values, then the declared return type can be an int array. A method can declare only one return value. Q: Can a method declare multiple return values? Or is there some way to return more than one value?Ī: Sort of. Stay tuned, though, we’ll have lots more to say about this. So if you pass a reference to an object into a method, you’re passing a copy of the remote control. And remember, you don’t stuff objects into variables the variable is a remote control- a reference to an object. Q: What happens if the argument you want to pass is an object instead of a primitive?Ī: You’ll learn more about this in later chapters, but you already know the answer. And that something must be a value of the appropriate type. A variable with a type and a name, that can be used inside the body of the method.īut here’s the important part: If a method takes a parameter, you must pass it something. And a parameter is nothing more than a local variable. An argument (a value like 2, “Foo”, or a reference to a Dog) lands face-down into a. A caller passes arguments.Īrguments are the things you pass into the methods. So you can call them whatever you like (arguments, donuts, hairballs, etc.) but we’re doing it like this:Ī method uses parameters. Although there are formal computer science distinctions that people who wear lab coats and who will almost certainly not read this book, make, we have bigger fish to fry in this book. You might, for example, want to tell a Dog object how many times to bark by calling: d.bark(3) ĭepending on your programming background and personal preferences, you might use the term arguments or perhaps parameters for the values passed into a method. Just as you expect from any programming language, you can pass values into your methods.
