Methods Flashcards
public static void main (String []args)
Identify the parts of the method.
void - return type
main - method name
(String []args) - parameter
What is the format of a method?
public static return_type methodName ([parameters])
It is a variable that will receive the value being passed in calling the method.
Parameters
This return type will not return any value.
void
a collection of statements that are grouped together to perform an operation.
Method
a collection of statements that are grouped together to perform an operation.
Method
Examples of defined methods in the Java library
- System.out.println
- Integer.parseInt,
- Double.parseDouble
- System.exit
- Math.pow
- Math.random.
Syntax in declaring method
[accesskeyword] static returntype methodname (parameter) {
}
can be public, private, or protected.
Access keyword
What will be the default form of access if no access modifier is declared?
Package
indicates the type of value returned or reported back to the calling method, and it is most often used in conjunction with the assignment of a value to a variable in the calling method.
Return type
What can the return type be?
- any Java primitive data type
- an object
- void
This return type denotes hat the method has no return value
Void
What naming rules does the method name follow?
Follows the same naming rules as variables and will generally begin with a lowercase letter.
What are methods and variables caused because they are both contained inside classes?
They are called class members
receives the value being passed when a function is called
Parameter
What is used to separate parameters in a parameter list?
Comma
states the data type of the parameter, which can be either a primitive data type or an object.
First entry in a parameter
declares the name to be used when referencing the parameter within the method.
Second entry in a parameter
a variable inside the method
Parameter
Name the parts of the method
public static int max(int num1, int num2)
public static int max(int num1, int num2) - method header
public - access keyword
static - modifier
int - returntype
max - method name
(int num1, int num2) - parameter list
num1 & num2 - parameter
What would the output be?
public static void main(String[] args) {
System.out.println(“Main Methods”);
DisplayMessage();
System.out.println(“Back to Main Methods”);
}
static void DisplayMessage(){
System.out.println(“Programming is great fun!”);
}
Main Methods
Programming is great fun!
Back to Main Methods
What would the output be?
public static void main(String[] args) {
DisplayNumbers();
DisplayMessage();
}
static void DisplayMessage(){ System.out.println("Programming is great fun!"); } static void DisplayNumbers(){ System.out.println("One"); System.out.println("Two"); System.out.println("Three"); }
One
Two
Three
Programming is great fun!
TRUE OR FALSE: Methods can also be called in a hierarchical, or layered fashion.
TRUE