O’Reilly members endure live online training, to add books, videos, and digital content from 200+ publishers.

You are watching: A called method accepts the value of an argument passed to it as its ____.


State influence behavior, actions affects state. We recognize that objects have actually state and behavior, stood for by instance variables and methods. But until now, we haven’t looked at how state and also behavior are related. We already know the each instance of a class (each object of a details type) can have that own unique values for its instance variables. Dog A have the right to have a name “Fido” and a weight of 70 pounds. Dog B is “Killer” and also weighs 9 pounds. And also if the Dog class has a technique makeNoise(), well, don’t you think a 70-pound dog barks a little deeper 보다 the tiny 9-pounder? (Assuming the annoying yippy sound have the right to be considered a bark.) Fortunately, that’s the whole suggest of an object—it has behavior that acts on its state. In various other words, methods use instance variable values. Like, “if dog is much less than 14 pounds, do yippy sound, else...” or “increase load by 5”. Let’s go change some state.

Remember: a course describes what an object knows and what things does

A class is the blueprint for an object. when you write a class, she describing exactly how the JVM should make an object of that type. You currently know the every object of that form can have different instance variable values. But what about the methods?


Can every thing of that form have different technique behavior?

Well... sort of.*

Every instance of a details class has the exact same methods, however the methods can behave differently based on the value of the circumstances variables.

The Song course has two circumstances variables, title and also artist. The play() an approach plays a song, however the circumstances you call play() on will certainly play the song represented by the worth of the title circumstances variable for that instance. So, if you call the play() method on one circumstances you’ll hear the track “Politik”, while one more instance dram “Darkstar”. The an approach code, however, is the same.

void play() soundPlayer.playSound(title);


Song t2 = new Song();t2.setArtist("Travis");t2.setTitle("Sing");Song s3 = brand-new Song();s3.setArtist("Sex Pistols");s3.setTitle("My Way");
The dimension affects the bark
A small Dog’s bark is different from a huge Dog’s bark.

The Dog course has an instance variable size, the the bark() method uses to decision what sort of bark sound come make.



You can send points to a method

Just together you suppose from any programming language, you can pass values into your methods. Girlfriend might, because that example, want to phone call a Dog thing how countless times come bark by calling:

d.bark(3);Depending on your programming elevator and an individual preferences, you could use the ax arguments or perhaps parameters for the worths passed into a method. Although over there are formal computer science distinguish that people who wear laboratory coats and also who will practically certainly not review this book, make, we have actually bigger fish to fried food in this book. So you can call them every little thing you favor (arguments, donuts, hairballs, etc.) but we’re doing it favor this:

A method uses parameters. A caller overcome arguments.

Arguments are the things you pass into the methods. An argument (a value like 2, “Foo”, or a reference to a Dog) soil face-down right into a... Wait for it... parameter. And also a parameter is nothing an ext than a local variable. A variable with a kind and a name, that deserve to be provided inside the human body of the method.

But this is the crucial part: If a an approach takes a parameter, you must pass that something. and that something need to be a value of the ideal type.

See more: 30 Gallon Drum For Sale Near Me Tal Barrels For Sale Near Me

Methods have the right to return values. Every an approach is claimed with a return type, but until now we’ve made every one of our methods with a void return type, which way they don’t give anything back.

void go() But we have the right to declare a method to provide a specific form of value earlier to the caller, together as:

int giveSecret() return 42;If you explain a technique to return a value, friend must return a value of the claimed type! (Or a worth that is compatible v the claimed type. We’ll get into that an ext when us talk about polymorphism in Chapter 7 and also Chapter 8.)