YuWebdesign



Methods in Java

By YuwebDesign

  1. You can pass data, known as parameters, into a method.
  2. Inside of a method we have statement(s)
  3. Methods define the actions that can be performed on classes.
    Sometimes they are also known as functions.
  4. Name of a method should be a verb
    that starts with a lowercase letter
    and describes best the action the method performs.
  5. Keep a method as short as possible.
    As a rule of thumb to up to 3 lines with 20 lines as a maximum.

Syntax:

method(parameters){
//method definition
}

A method is a block of code which only runs when it is called.

Inside main, call the myMethod() method:

public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}

public static void main(String[] args) {
myMethod();
}
}

// Outputs "I just got executed!"

  1. Information can be passed to functions as a parameter inside the parentheses.
  2. You can add as many parameters as you want, separating them with a comma.
    Recommendation is however to use the least amount of parameters possible,
    with 3-5 parameters as a maximum.

E.g., method that takes a String called fname as parameter.
When the method is called, we pass along a first name,
which is used inside the method to print the full name:

public class MyClass {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}

public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes

A method uses parameters.
A caller passes arguments.

When we define a method, we give it parameters.
When we call a method, we give it arguments.

  1. Arguments are what you pass into methods.
    An argument (a value like 2, “Foo”, or a reference to a Dog) lands into a parameter.
    Arguments are a part of a calling of a method.
  2. A parameter is nothing more than a local variable that is going to store that data.
    A variable with a type and a name, that can be used inside the body of the method.
    Parameters are a part of definition of a method.

  1. static method can be accessed without creating an object of the class
  2. public can only be accessed by objects

public class MyClass {
static void myMethod() {
// code to be executed
}
}

  1. myMethod() – name of the method
  2. static means that the method belongs to the MyClass class
    and not an object of the MyClass class.
  3. void means that this method does not have a return value.

public class MyClass {
static int myMethod(int x, int y) {
return x + y;
}

public static void main(String[] args) {
System.out.println(myMethod(5, 3));
}
}
// Outputs 8 (5 + 3)

You can also store the result in a variable (recommended):

public class MyClass {
static int myMethod(int x, int y) {
return x + y;
}

public static void main(String[] args) {
int z = myMethod(5, 3);
System.out.println(z);
}
}
// Outputs 8 (5 + 3)

  1. The void keyword indicates
    that the method should not return a value.
  2. To return a value,
    a primitive data type (such as int, char, etc.) instead of void,
    and the return keyword inside the method should be used.

Objects have state and behavior, represented by instance variables and methods.

  1. Curry methods return a value but do not alter the state.
  2. Command methods alter the state but do not return a value.

Method signature is the method name and the number, type and order of its parameters.

Return types and thrown exceptions are not considered to be a part of the method signature.

A class cannot have two methods with same signature. If we try to declare two methods with same signature you will get a compile time error.

A method signature is the smallest type of a method.
Read more on type signature


Leave a Reply or Comment

Your email address will not be published. Required fields are marked *