Home > Flashcards > Print Preview
The flashcards below were created by user
on FreezingBlue Flashcards. What would you like to do?
Methods help you ___________ your code.
Methods help you ____________ reusable code segments and avoid redundant code.
What are the four components of a method?
- 1) name
- 2) parameters (optional)
- 3) return type (can be void)
- 4) method body
How are methods executed?
Methods reside outside of the main program (main()). Other methods are called by name and passed any needed parameters.
If the method returns a value, a variable to hold the return value can be ___________ and ______________ at the same time.
What is another word for calling a method?
Must methods return a value?
What is the syntax for declaring a method?
- modifier (public, private, static, final, etc)
- return type (String, double, int, etc.)
- name (method name in camel case)
- parentheses (whether or not parameters are accepted)
- parameter list (list of parameters passed to the method when invoked - syntax: parameter type parameter name)
- body (between curly braces)
Review method example slides (there are a lot).
(pages 6-15, 16-19 of powerpoint)
Why are methods used?
- encapsulate repetitive code
- make programs neater and easier to read
- minimize errors
What does it mean if a method is overloaded?
- A method is overloaded if more than one method exists with the same name but each has distinct variable lists.
- An example of a good time to do this is max (int num1, int num2) and max (double num1, double num2).
What type of error should you anticipate if you have an overloaded method?
The ambiguous invocation error occurs when Java cannot determine which version of an overloaded method should be used. For example, if there are two versions of a method, (one accepts one int and one double, one accepts one double and one int), and two ints are passed when the method is invoked, Java will throw an exception.
Can you overload based on return type?
Can you overload a method with the same name, parameter number and parameter types?
Not in the same class.
What is variable scope?
It is the part of the program where the variable is known and can be referenced.
Variables are local to the _____________.
block they are declared in
A variable can be declared multiple times in ________________, but only once in _______________.4
- non-nested blocks
- nested blocks
Parameter names are ______________ to the method.