CHAPTER 9

image

Methods

Methods are reusable code blocks that will only execute when called.

Defining methods

A method can be created by typing void followed by the method’s name, a set of parentheses and a code block. The void keyword means that the method will not return a value. The naming convention for methods is the same as for variables – a descriptive name with the first word in lowercase and any other words initially capitalized.

class MyApp{   void myPrint()  {     System.out.print("Hello");  } }  

Calling methods

The method above will simply print out a text message. To invoke (call) it from the main method an instance of the MyApp class ...

Get Java Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.