What is a Java method?
A method is a named block of Java code that runs when it is called. It supports procedural abstraction by hiding implementation details behind a usable operation.
Study Java Methods and Program Design with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a Java method?
A method is a named block of Java code that runs when it is called. It supports procedural abstraction by hiding implementation details behind a usable operation.
What are the main parts of a method declaration?
A method declaration commonly includes an access modifier, return type, name, parameter list, and body.
What does void mean as a method return type?
A void method performs an action but produces no value, so its call cannot be used in an expression or assigned to a variable.
How do parameters differ from arguments?
A parameter is a variable declared in a method header; an argument is the value supplied for that parameter when the method is called.
What happens when a primitive argument is changed inside a method?
Java passes primitive arguments by value, so the method receives a copy. Changing its parameter does not change the caller’s original variable.
What can a method do with a reference-type argument?
Java passes a copy of the reference. The method can change the shared object’s contents, but assigning a new object to the parameter does not change the caller’s reference.
What does return do in a non-void method?
A return statement ends the current method and sends a value to the caller. Every possible path of a non-void method must return a compatible value.
What constitutes a Java method signature?
A method’s signature consists of its name and ordered list of parameter types. Its return type is not part of the signature.
What is method overloading?
Overloading means defining methods with the same name but different parameter lists. Java selects the applicable overload using the number and types of arguments, not the return type.
What determines the scope of parameters and local variables?
A parameter’s scope is the entire body of its method. A local variable’s scope extends from its declaration to the end of its enclosing block.
How does this resolve a field–parameter name conflict?
When a parameter has the same name as a field, the parameter takes precedence in the method. this.name refers specifically to the current object’s field.
What is the difference between a precondition and a postcondition?
A precondition must be true before a method runs; a postcondition must be true after it finishes. Together they form a contract between caller and method.