0% found this document useful (0 votes)
164 views

Methods

Methods in Java are defined within classes and have two types: static methods which perform tasks for the programmer, and instance methods which manipulate class data. A method definition includes the visibility, whether it is static, its return type, name, parameters, and body contained within curly brackets. When a method is called, its statements are executed sequentially. The main method acts as the entry point and can call other methods to perform tasks like printing output.

Uploaded by

api-446695789
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

Methods

Methods in Java are defined within classes and have two types: static methods which perform tasks for the programmer, and instance methods which manipulate class data. A method definition includes the visibility, whether it is static, its return type, name, parameters, and body contained within curly brackets. When a method is called, its statements are executed sequentially. The main method acts as the entry point and can call other methods to perform tasks like printing output.

Uploaded by

api-446695789
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Methods in Java

Methods
• A method is a piece of java code that does a job
• Defined within the curly brackets of a class
definition
• Two types
– 1. Static methods (also called class methods) do a job
for a programmer
– 2. Instance methods manipulate the data within a class
– For now we will talk about static methods
• Methods are sometimes called functions
The parts of a method
1. Visibility
• Public - any part of the Java program can use it
• Private - can only be used within the same class
2. Type of method (static?)
3. Return type (what type of information it returns)
4. Name
5. Parameters - Data given to the method to do its
job.
6. Body - The statements that get executed when
the method is called.
Parts of a Method
2. Static3. Return type (void means
1. Visibility nothing returned)

4. Method name

public static void main (String[] args) 5. List of


parameters
{
6. Body
System.out.print(“Hello world”);
inside curly
} brackets
Indenting Methods and Functions
• Statements inside the void myMethod(){
method or function must statement1
be indented
• Close bracket at end
}
should be indented the
same as the definition
• Open bracket can be on
same line or next line
Running a Java method
• The body of a method between the curly brackets
contain one or more statements
• Statements are separated from one another by
semicolons
• When a method is run, it executes the statements
one at a time
• When Java is run, it looks for a method called
main and runs it
• The main method can call other methods, either in
the same class or in other classes.
Calling a Java method
To call a method, use the class name followed by a .
followed by the method name
class theClass {
public static void main(String[] args) { In the example, the
theClass.theMethod(); main method calls on
} theMethod
public static void theMethod() {
System.out.print(“hello”); What does this program
}
print?
}
Some common statements
• System.out.println(“something to print”);
– System.out.println prints whatever is inside the
parentheses to the console
– Moves the console cursor to a new line when it
is done.
• System.out.print(“something else”);
– Similar to System.out.println, but does not
move the cursor to a new line.
Summary
• Two kinds of methods
– Static methods do work for the programmer
– Instance methods manipulate data (we won’t talk about these)
• Method definitions have multiple parts.
• The body of a method contains one or more statements.
– End with a semicolon
– Are executed in order when the method is run
• The main method is run when a Java program starts
• System.out.print/println send output to the console

You might also like