0% found this document useful (0 votes)
8 views8 pages

Java Methods: Definition and Structure

A method is a container for code that performs some specific action. It accepts data as parameters, performs operations on that data, and returns a result. Methods have a name, parameters in parentheses, code within curly braces, and may specify a return type like int or void depending on whether they return a value. Access modifiers like public and private control whether a method can be called from within or outside the class.

Uploaded by

Roshni Dash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Java Methods: Definition and Structure

A method is a container for code that performs some specific action. It accepts data as parameters, performs operations on that data, and returns a result. Methods have a name, parameters in parentheses, code within curly braces, and may specify a return type like int or void depending on whether they return a value. Access modifiers like public and private control whether a method can be called from within or outside the class.

Uploaded by

Roshni Dash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Understanding Java Code

Method
A container for our code.
What is a method ?
Contains a set of code that performs some action.

Code

Container

Method is also called as a function.

It is a black box where you can pass some data and get an output.

Method accepts data, performs some manipulation on it and gives us back data.
A method looks like:
Parenthesis(to pass data)

Method Name myMethod(){ Opening Curly Brace(Indicates start of the


method)
//code

Closing Curly Brace(Indicates end of the


method)
Return Type
Every method should have a
return type.
What is a return type ?

int myMethod(){ void myMethod(){

//code //code

} }
Access Modifier
What is an access modifier ?
Something that can modify access of a method.

A method can be accessed inside our outside of a class depending upon its access
modifier.

You might also like