Java Methods For Lucky Day 13

in #java6 years ago

Its Time For More On Java Methods

ai-close-up-code-160107.jpg
Image courtesy of pexels.com

We have been working through Java Object Oriented Programming over the past few days with days with a further clarification of Java Classes yesterday. Today is a further deep dive on Java Class Methods to give more information on how they work with our objects in Java.

A method is a set of commands which allow you to perform a specific operation. A method is a function and gives your class functionality and perform actions on the objects data.

To return a methods result in Java, we use the return keyword. The return statement doesn't just return the methods return value, it also stops the method from running. When we declare a method we declare it as public and then need to provide the return value type, for example, if it returns an integer number, we set it as int, if it does not return anything we set it as void.

We can also pass arguments to a method when call it. The declaration of the method includes a list of variables which lets us know the order and type of variables the method needs. The order is also important with these variables list called the method parameters.

Method overloading is a way to add multiple methods with the same name in case there are more than one set of arguments you may need to pass to it. We could take this further and we could use the (String...names) option in our parameters as you will see in our example code below. This declares we will get one or more number of parameters and then create our method to cover that. As we read yesterday constructors are a type of method and they can also be overloaded.

Variables parsed as parameters are only copies of the variable and will not be changed by the method. If an object is passed to a method and not a variable value they are passed by reference, which is a reference to the same area of memory where the object is stored and as a result can make changes. If you pass an object to a method as an argument and change the values, the values will be changed.

Code Of The Day

  1 public class Greetings {
  2 
  3     public void sayHi(String...names) { // Function can take one or more arguements
  4 
  5         for (String name: names) {
  6             System.out.println("Good evening, " + name + ". How are you?");
  7         }
  8     }
  9 
 10     public static void main(String[] args) {
 11         Greetings greetings = new Greetings();
 12         greetings.sayHi("John Smith", "Captain America", "Some Other Guy");
 13     }
 14 }

Output

java Greetings
Good evening, John Smith. How are you?
Good evening, Captain America. How are you?
Good evening, Some Other Guy. How are you?

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 11 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/objects-in-day-11-of-one-month-of-java

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63901.15
ETH 3133.40
USDT 1.00
SBD 4.05