O’Reilly members endure live digital training, plus publications, videos, and also digital content from 200+ publishers.

You are watching: A method can return nothing, in which case the method is a ____ method.


*

State affects actions, actions affects state. We know that objects have state and behavior, represented by instance variables and also methods. But until currently, we haven’t looked at just how state and also habits are related. We already recognize that each circumstances of a course (each object of a specific type) deserve to have its very own distinctive worths for its circumstances variables. Dog A can have a name “Fido” and a weight of 70 pounds. Dog B is “Killer” and also weighs 9 pounds. And if the Dog class has actually a technique makeNoise(), well, don’t you think a 70-pound dog barks a bit deeper than the bit 9-pounder? (Assuming that annoying yippy sound have the right to be considered a bark.) Fortunately, that’s the totality allude of an object—it has actually behavior that acts on its state. In other words, methods use instance variable values. Like, “if dog is much less than 14 pounds, make yippy sound, else...” or “increase weight by 5”. Let’s go adjust some state.


Remember: a class explains what an item knows and what a things does

A course is the blueprint for an object. When you create a course, you’re describing just how the JVM must make an object of that kind. You currently recognize that eextremely object of that kind have the right to have various instance variable values. But what around the methods?


*

Can every object of that form have different approach behavior?


Well... kind of.*

Eexceptionally circumstances of a specific class has the very same techniques, however the methods can behave in a different way based upon the worth of the circumstances variables.

The Track course has actually 2 instance variables, title and artist. The play() method plays a song, however the instance you contact play() on will certainly play the song stood for by the value of the title instance variable for that instance. So, if you contact the play() method on one instance you’ll hear the song “Politik”, while another circumstances plays “Darkstar”. The approach code, however, is the exact same.

void play() soundPlayer.playSound(title);

*

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

The Dog class has an circumstances variable size, that the bark() method provides to decide what kind of bark sound to make.


*

*

You have the right to sfinish points to a method

Just as you mean from any type of programming language, you can pass worths right into your methods. You can, for instance, want to tell a Dog object just how many times to bark by calling:

d.bark(3);Depfinishing on your programming background and personal preferences, you might usage the term arguments or maybe parameters for the worths passed right into a method. Although tbelow are formal computer system scientific research distinctions that civilization that wear lab coats and also who will certainly almost absolutely not review this book, make, we have actually bigger fish to fry in this book. So you deserve to call them whatever you like (debates, donuts, hairballs, and so on.) however we’re doing it choose this:

A approach uses parameters. A caller passes arguments.

Arguments are the points you pass into the techniques. An argument (a value prefer 2, “Foo”, or a referral to a Dog) lands face-dvery own into a... wait for it... parameter. And a parameter is nothing more than a neighborhood variable. A variable via a kind and a name, that have the right to be offered inside the body of the method.

But here’s the crucial part: If a method takes a parameter, you must pass it something. And that something must be a worth of the proper type.

See more: How Do You Say First In Italian Words And Basic Phrases, How To Say First In Italian


Methods have the right to rerotate values. Eincredibly approach is declared through a rerevolve kind, but until now we’ve made all of our approaches with a void return type, which implies they don’t give anypoint back.

void go() But we have the right to declare a method to provide a details type of value earlier to the caller, such as:

int giveSecret() rerotate 42;If you declare an approach to return a value, you must rerotate a worth of the claimed type! (Or a value that is compatible with the declared type. We’ll acquire right into that more as soon as we talk about polymorphism in Chapter 7 and also Chapter 8.)