top of page

Declaring a Method in the Java Class

  • javastrokes
  • Dec 7, 2016
  • 2 min read

In the previous post, we declared fields/variables inside class declaration. Now we are going to declare methods in a Java class. A method in a Java class is used to define behaviour of the objects of that class or the behaviour of the class itself. A method is a named block of code.

Four parts of method declaration:

the return type,

method name,

a pair of opening and closing parentheses,

& a pair of opening and closing braces

- The return type of a method is the data type of the value that the method will return when it is invoked. ( It could be a primitive data type (for example, long, float, boolean, etc.) or a reference type (for example, Employee, Product)

or a method does not return a value to its caller (in some cases)

- method name must be a valid Java identifier.

- A method may take input values from its caller(an another method). A parameter is used to take an input value from the caller. You can declare a parameterized method with one or more parameters. A parameter is nothing but a variable declaration which consists of two parts: a data type and a variable name.

Example:

int add ( int n1, int n2 ) { int sum = n1 + n2; return sum; }

The code (business logic) of Java class method is executed during method invocation. A method or code that invokes some other method is called the caller of that method. A method may accept input values (Optional) from the caller and it may return a value to the caller.

Caller (ex: an another method) could provide values to your method, which are called as input values known as parameters. A method may have zero or more than one parameters.

Example:

public class MethodDeclaration { /* The method is named "add"; it takes two parameters of type int named n1 and n2 and it returns their sum: Note: A method may take input values from its caller. four parts in a method declaration are mandatory: the return type, method name, a pair of opening and closing parentheses, & a pair of opening and closing braces. A method is uniquely identified by its signature in a particular context */ int add(int n1, int n2) { int sum = n1 + n2; return sum; // evaluates the expression and returns the value & gives the //control to //the caller of the method. } public static void main(String[] args) { MethodDeclaration md = new MethodDeclaration(); /*A method is invoked using its name with the values for its parameters & we capture the returned value of a method ... */

int sum = md.add(10, 12); System.out.println("Sum of add method is :" + sum); /* The two values, 10 and 12, that are used to call the add method are called actual parameters. Java copies the actual parameters to the formal parameters before it executes the code inside the body of the method. * */ } }

Comentarios


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Recent Posts
Archive
Search By Tags
  • Grey Facebook Icon
  • Grey Twitter Icon
  • Grey Instagram Icon

© 2016 by JavaStrokes. Proudly created with Wix.com

Success! Message received.

© Copyright 2016 by JavaStrokes
bottom of page