Java Exam 5.4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Identify whether each phrase describes local variables or class variables (fields), or both. 1) Declared in a class, right after open brace for class - must be declared static when methods using field are static. 2.Declared in a method or block or parameter list. 3.Hold(s) input values and return values. 4.Mostly used for constants, variables that never change from their initial value.

1.class 2.local 3.both 4.class

Identify whether each phrase describes local variables or class variables (fields), or both. 1.Effectively belongs to the block in which they are declared and not visible outside that block. 2.Should be declared private to be only used by code in the class. 3.Uses private modifier. 4.Always named with all lowercase with first letter of each word in uppercase. 5.Should be descriptive of purpose.

1.local 2.class 3.class 4.both 5. both

A method signature is made up of the method __________ and the ______ ______ of the _______ ________.

1.name, data types, method's parameters

Method overloading occurs when two or more methods in a class may have the same _____ as long as their ____________ ________ are different.

1.name, parameter lists

Method overloading is useful when the __________ operation has to be performed in several _____________ ways.

1.same, different

The compiler binds to the correct version of the overloaded method through the method's ______________.

1.signature

A method's local variables exist only whilet.

the method is executing. When the method ends, the local variables and parameter variables are destroyed and any values stored are los

A method signature consists of

the method's name and the data types of the method's parameters, in the order that they appear. The return type is not part of the signature. Below are the signatures of the add methods from the code above. add(int, int) add(double, double)

Parameter variables declared in a method's parameter list are also local to

the method.

Different methods can have local variables with the same names because

the methods cannot see each other's local variables.

True or False: Fields and local variables can have the same name.

True

Each parameter variable is initialized with the corresponding _____ value when the method is called.

argument

Local variables are not

automatically initialized with a default value and must be given a value before they can be used.

Local variables belong to a(n) ___________. They must be _____ since they have no default value.

method initialized

A local variable declared inside a method is a

method-level variable and is not accessible to statements outside the method.

Methods in a program that have the same name are called

overloaded methods. Overloaded methods are helpful when you need to have a method handle or process different types of data in the same way.

Parameter variables are created in a method's __________________.

parameter list

A class variable or class field is declared at

the class-level right after the class header, following the open brace for the class. It is accessible to all statements (methods) within the class. It is declared using access specifiers

functional decomposition

the process of breaking a problem down into smaller pieces [and has basis in the concept of divide and conquer]1.

In the same class, class-level and method-level variables can have

the same name, but the method-level variables take precedence, thereby shadowing (hiding) the field. To access a static class-level variable (field) in a static method, use the name of the class followed by a dot operator and the field name. Example: Scope.x refers to field x in the Scope class.

the Math class, as other classes in the Java API,

uses overloaded methods for a number of functions. The max methods return the greater of two values, but each set is for a different data type. The same applies to the min method.

Variables declared at the method-level are

variables that are local to the method. They are scoped (belong) to the method only. No other code in the same class can access a method's variables.

What are the signatures for these headers? public static long round(double a) public static int round(float a)

round(double) round(float)

If a method calls another method that has a throws clause in its header, then the calling method

should have the same throws clause.

Problem Solving with Methods

A large, complex problem can be solved a piece at a time by methods.

Calling Methods that Throw Exceptions

All methods that use a Scanner object to open a file must throw or handle IOException. For now, understand that Java [requires]2 any method that interacts with an external entity, such as [a]3 file system to either throw an exception to be [handled]4 elsewhere in your application or to handle the exception locally.

Local Variables and Scope

Another rule that you must remember about local variables is that you cannot have two local variables with the same name in the same scope.

Declare as constant for values that can't change during program execution.

Field name in all uppercase. Join multiple words in field name with underlines. private final int LEGAL_DRINKING_AGE = 21; private static final int LEGAL_DRINKING_AGE = 21;

Shadowing

Any block may contain variable declarations. If a local variable or parameter in a method has the same name as a field of the class, the class field is "hidden" until the block terminates execution. A static field that is shadowed by a local variable within a method of the same class can be accessed using the class name.1

Methods with the Same Name

Sometimes it is necessary to use the same method name multiple times in the same class. Take for example a method that performs an addition. In Java there are several numeric data types on which to perform an addition. Method names are descriptive of what the method does, so it makes sense to reuse the method name. You might ask how that is possible since you can't code multiple fields with the same name or multiple local variables with the same name? The answer is in the parameter list.

Local variables are not automatically initialized with

a default value and must be given a value before they can be used.

access specifiers: private

which means only the statements inside the class can use the field and is primarily used when declaring class fields. Useful in promoting data hiding. Protects variables from corruption or malicious alteration.

A method's local variables exist only

while the method is executing. When the method ends, the local variables and parameter variables are destroyed and any values stored are lost.

In the code below, describe the parameter variable and the local variable public static double convertKmToMi(double kilometers) { double miles = kilometers * 0.621371; return miles;}

•kilometers is the parameter variable •miles is the local variable as it is declared in the body of the method •both are considered local to the method

method overloading.

◦Two or more methods in a class may have the same name as long as their parameter lists are different. Method overloading is important because sometimes you need several different ways to perform the same operation.

access specifiers: protected

- which means statements in the same family of classes can use the field.

access specificers: public

- which means statements inside or outside the class can use the field.

static binding.

The process of matching a method call with the correct method The compiler uses the method signature to determine which version of the overloaded method to bind the call to.

For the computer to know which method to call or bind to, the method signature has to

be different.

Variables declared at the class-level are

class variables or fields. They are scoped (belong) to the class. All the code within the class can use these variables. Should be always declared as private unless told otherwise.

Class variables are automatically initialized when

declared private int age; //where age will be initialized to 0. OR private static int age; //where age will be initialized to 0.

A local variable is

declared inside a method and is not accessible to statements outside the method.

The unintentional shadowing of fields can cause

elusive bugs, so you need to be careful about giving local variables the same names as fields.1 And when you do, make sure you're fully cognizant of which one is the local and which one is the field so that the correct variable is being processed. If the wrong variable is processed, then the wrong value is used in delivering incorrect results.2

Suppose a class has a field declared as: private static double width; and then a method within the class has a local variable declared as: double width = 0.0; Following the initialization of the local variable width, this variable shadows the name of the _____.

field

Variables can also be local to logical control structures such as

if, if-else, switch, and any of the repetition structures as long as they are declared inside the braces of each structure.1

Declare as static if

methods in the same class that use the field are static.


संबंधित स्टडी सेट्स

AP Lit: How to Read Poetry Like a Professor Test

View Set

ATI Lifespan & Development-Human Growth & Development

View Set

Vocabulary Lesson 8: Help and Improvement

View Set

Access Modules 1-3 SAM Training - ISDS 1100

View Set

Security Plus Attempt 4 Vocab Review Guide 2.0

View Set

Econ 320 Exam 2 Practice Questions

View Set