When should you use instance variables, parameters, and local variables? List at least one mistake that you can make when misusing one type of variable for another.

What will be an ideal response?


The only reason to use an instance variable is to store information within an object. The
only reason to use a parameter is to transmit information to a method. The only reason to use a local variable is for temporary working storage within a method. A very common mistake is to misuse one kind of variable for another.

Parameters are preferable over instance variables because: if several methods share a pool of variables, and one method misuses a variable, other methods can be affected; it is easier to understand methods and the relationships between them when communications are explicitly defined in terms of parameters and return values; methods that are passed parameters can be reused in many situations.

Common mistakes include:
1. Instance variable used for temporary working storage.
2. Local variable used to remember information in an object.
3. Method accesses data by directly referencing an instance variable when it could use a parameter instead.

Computer Science & Information Technology

You might also like to view...

If the following pseudocode was coded and executed, what would display?

``` Declare String str = "ABC" Display str[1] ``` a. A b. B c. C d. ABC

Computer Science & Information Technology

With the frame value ____, a border is drawn only above the table.

A. top B. above C. hsides D. box

Computer Science & Information Technology

If you decide to play a slideshow automatically and have it display full screen, you can drag the video frame to the gray area off the slide so that it does not display briefly before going to full screen.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

What are the difficulties associated with wireless network connections?

What will be an ideal response?

Computer Science & Information Technology