Ad space (banner)
โ˜•Java Lessons
Lesson 6 / 68

Writing a Method (Function)

This lesson covers writing a method (function) in Java, so you can write code that's reused across many calls. It's for anyone searching "Java method tutorial."

In Java, a function is called a method. Define one in the form return-type methodName(parameters). Bundling logic you use often under a name means you can just call it whenever you need it.

The example defines a method, greet, that takes a name parameter and returns a greeting message as a String. Like a recipe you write once, naming a set of steps lets you reproduce the same result any time you need it.

A common early mistake is not following method naming conventions. Method names should typically start with a verb โ€” like getName() โ€” so the name itself conveys "what this does"; skipping this convention leads to confusing names.

In real projects, bundling shared logic into a method reduces duplication and means a fix only needs to happen in one place.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)