๐Ÿ“š

ย >ย 

๐Ÿ’ปย 

ย >ย 

โœ๏ธ

How to Write Method Headers

2 min readโ€ขdecember 21, 2021

Milo Chang

Milo Chang

Milo Chang

Milo Chang


AP Computer Science Aย ๐Ÿ’ป

130ย resources
See Units

Look at this chunk of code: ๐Ÿ’ป
public class Cats { ย  ย public int countNumFeet (int numCats) ย  ย { ย  ย  ย  ย  return 4*numCats; ย  ย } โ€‹ ย  ย // There may be instance variables, constructors, and other methods not ย  ย // shown. }
Can you identify the method header? ๐Ÿค”
That's right! ๐Ÿ‘ It's "public int countNumFeet (int numCats)"! ๐Ÿฑ
Breaking it Down: ๐Ÿ”
The keyword "public" means that this method can be accessed from other classes.
  • It can be replaced by the word "private" to give you:
private int countNumFeet (int numCats)
  • Putting "private" means that the method can only be accessed within the "Cats" class
The keyword "int" tells you what this method returns when it is called.
  • In this case, it returns an integer value after it is called
  • It can be replaced by "double", "boolean", "String", or the name of a class if the method returns an object
  • If the word "void" is there instead of the other options, this means the method doesn't return anything
The word "countNumFeet" is the name of the method.
  • This can be replaced with anything without changing what the method does
  • It's best if you make the name of the method something that lets people easily know what it's doing
The "int numCats" tells you what is being passed into this method.
  • In the example, an integer called "numCats" is being passed in
  • You can have a method that has nothing being passed in:
public int countNumFeet ()
  • You can also have a method that has multiple parameters passed in:
public int countNumFeet (int numCats, int numEars, boolean isAngry)
โ€‹ Method headings may also include the phrases "static" or "abstract", but this will be discussed in later articles.ย  โ€‹
Try it out! ๐Ÿ˜ƒ
#1) Write the header for a method that can only be accessed within the class it's declared in. This method takes in a String called "text" and doesn't return anything. Call your method "example1". ย (Scroll down for the answer)
#2) Write the header for a method that can be accessed from other classes. This method takes nothing in and returns a boolean. Call your method "example2". (Scroll down for the answer)
#3) Write the header for a method that can be accessed from other classes. This method takes in a boolean "testPassed" and an integer "score". It returns a String. Call your method "example3". (Scroll down for the answer)
Answers: โœ”
#1) private void example1 (String text)#2) public boolean example2 ()#3) public String example3 (boolean testPassed, int score)
โ€‹
Summary: ๐ŸŽ‰โœจ
You can write a method header with just a few simple steps.
  1. Chooseย publicย orย privateย 
  2. Choose what the method returns:ย voidย (nothing),ย int,ย double,ย boolean,ย String, or the name of a class
  3. Choose the name of the method
  4. Choose what parameters to pass into the method
That's it! You're ready to start writing your own method headers! ๐Ÿ‘
Browse Study Guides By Unit
โž•Unit 1 โ€“ Primitive Types
๐Ÿ“ฑUnit 2 โ€“ Using Objects
๐Ÿ–ฅUnit 3 โ€“ Boolean Expressions & if Statements
๐Ÿ•นUnit 4 โ€“ Iteration
โš™๏ธUnit 5 โ€“ Writing Classes
โŒš๏ธUnit 6 โ€“ Array
๐Ÿ’พUnit 7 โ€“ ArrayList
๐Ÿ’ปUnit 8 โ€“ 2D Array
๐Ÿ–ฒUnit 9 โ€“ Inheritance
๐Ÿ–ฑUnit 10 โ€“ Recursion
๐Ÿ™Exam Reviews