Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
ADVERTISEMENT
Home Java

Java Methods

2 years ago
in Java
Reading Time: 3min read
39 1
A A
40
SHARES
4k
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

SimilarPosts

Java Iterator

Java Iterator

2 years ago
4k
Java Iterable Interface

Java Iterable Interface

2 years ago
4k
Java Control Structures – Loops Introduction

Java Control Structures – Loops Introduction

2 years ago
4k
Java Operators – Logical Operators

Java Operators – Logical Operators

2 years ago
4k

A method is used to encpsulate a unit of reusable code.

Methods organize and simplify coding.

Method definition

A method definition comprises the following:

  1. Method name.
  2. Method Parameters.
  3. Return type.
  4. Method body
1
2
3
modifier returnType methodName(parameters) {
    // Method body;
}
Let’s write a simple method to add two numbes:
1
2
3
private int add(int num1,int num2) {
        return num1+num2;
    }
In this case:

  1. private – access modifier for the method.
  2. int – return type of the method.
  3. int num1,int num2 – method parameters.
  4. { return num1+num2} – method body.

Method Invokation

To call a method is also called to invoke a method.

Normally there are two situations you have to consider before you can successfully call a method.

  1. Instance Methods need an an instance of a class to be called.
  2. Static methods don’t need a class instance.
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;
 
public class MrMethod {
 
    public static void main(String[] args) {
        MrMethod mrMethod=new MrMethod();
        System.out.println("Result : "+mrMethod.add(253,347));
    }
    private int add(int num1,int num2) {
        return num1+num2;
    }
}
Result
1
Result : 600
For example, in the above example, we need to instantiate MrMethod class to be able to successfully invoke the add() method.

Then we call the mrMethod.add(253,347) to add.

On the other hand we can modify the method to become a static method:

1
2
3
private static int add(int num1,int num2) {
        return num1+num2;
    }

We use the static modifier. This now ties the method to the class as opposed to the class instance.

So we don’t need the instance of the class for successful invokation of this method:

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;
 
public class MrMethod {
 
    public static void main(String[] args) {
        System.out.println("Result : "+add(253,347));
    }
    private static int add(int num1,int num2) {
        return num1+num2;
    }
}

Result

1
Result : 600

Returning a Value

As you can see from the above examples, both methods return a value.

1
2
3
private static int add(int num1,int num2) {
    return num1+num2;
}

This method has to return an integer given that already we’ve specified that it will only be capable of returing that in the method definition.

The below method returns a String:

1
2
3
private static String getName() {
        return "Oclemy";
    }

We’ve specified that it should return a String so the value we return has to be a string.

1
2
3
4
5
6
7
8
public class MrMethod {
    public static void main(String[] args) {
        System.out.println("My name is : "+getName());
    }
    private static String getName() {
        return "Oclemy";
    }
}

Result

1
My name is : Oclemy

Method Parameters

Methods can take parameters or arguments. And indeed the first example we saw can attest to that.

1
2
3
private static int add(int num1,int num2) {
    return num1+num2;
}

In this case two integeres are passed: num1 and num2.
The parameters passed to a method are only visible within the method body.

Methods can take parameters of any type.

1
2
3
4
5
6
7
8
public class MrMethod {
    public static void main(String[] args) {
        System.out.println(getSpacecraftDetails("Swift","Plasma Ions"));
    }
    private static String getSpacecraftDetails(String name,String propellant) {
        return "Name: "+name+" Propellant: "+propellant;
    }
}
Result
1
Name: Swift Propellant: Plasma Ions
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Share16Tweet10Send
ADVERTISEMENT

Related Posts

Java Iterator
Java

Java Iterator

December 19, 2020
4k
Java Iterable Interface
Java

Java Iterable Interface

December 19, 2020
4k
Java Control Structures – Loops Introduction
Java

Java Control Structures – Loops Introduction

December 19, 2020
4k
Java Operators – Logical Operators
Java

Java Operators – Logical Operators

December 19, 2020
4k
Java Operators – Relational Operators
Java

Java Operators – Relational Operators

December 19, 2020
4k
Java URI
Java

Java URI

December 19, 2020
4k
ADVERTISEMENT

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In