O'Reilly logo

Java Quick Syntax Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required