Methods
Methods
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