Skip to main content

Structure of a Method in JAVA


Structure of a Method in JAVA

In this tutorial you will learn
1. Basic structure of a method
2. Input into methods
3. Output from methods
4. How Methods are used in JAVA program
Basic structure of a method
The function in C programming are referred to as Methods in JAVA. They are the set of instruction which are clustered together to perform some specific operation to the input they are given and then the data is returned back to the main of the program or to the Method in which they arr called. The data types of the input arguments and the outputs must be carefully decided because each data type has its cons and pros.
The structure of the method is :

  1.  
  2. Access specifier Return Type name_Of_Method( input arguments)
  3. {
  4. //body of the method
  5. }

Access specifier can any be of private, public and protected. The method perform an operation and return it back but according to the return type being used and may give an error if the return types are not appropriate.
Input into methods
We needs methods to perform some operation on the input data and return to us after performing some operation. The input arguments when given into the method have to be written with its data type. And whenever we want some variable to be put into a method it must be in accordance with the input data type of the method. Following are the data types used in JAVA programming.
• boolean
• byte
• short
• int
• long
• float
• double
• char

Each data types has its limitations and advantages like int can only have numbers while char datatype can have numbers and alphabets as well.

Example:
This example can give a clearer view

  1. Access specifier Return Type name_Of_Method( datatype input_argument_1,datatype input_argument_2// .. . . . )
  2. {
  3. //body of the method
  4. }

So when we are defining the Method we have to completely specify its input and output datatypes otherwise the compiler generates an error.

Outputs from Methods
If the output from the method has to be further used somewhere in program where we just do not display the output rather we make a variable in the main of the program which is equated with the method with the given inputs and it stores the output value in itself. But according to our need if we just want to display, we just use

  1. System.out.print(myfunction(a,b));

Hence the above program will just display the result rather saving it.
But if the method is made in such a format that it returns something that we want to save we write the code as
  1. Int a;
  2. a=add(c,d);

Here we have supposed that ‘add’ is the method already been made and after the addition of the variable c and d the result is saved in ’a’.

How Methods are used in JAVA program
The methods in JAVA can be called in main and in other methods as well. The access specifiers are used to limit the access of the methods. Whenever a method having return type void is to be called we do so as

  1. //calling method in main
  2. Myfunction(a);

Here return type is assumed to be void, so no variable is used on the left side of the function, but if there is some return type we then use a variable on the left side of the equation by which function and the variable is equated.

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.