Week 4, Notes 3 – Constants, parameters, and scope 

See W04N03.java in the demos directory for today.

 

Declaration of Constants

A constant is just like a variable, but once assigned, you cannot change its value.

Generally declared outside of any method and visible to all methods in the class.

Use one of the visibility modifiers, public or private

Example:

private static final double MAXLOANAMT = 300000.00; 

By convention, constants usually are named with all uppercase letters.

Passing values to methods using parameters

Parameters are the way information is transferred (passed) from the place a method is called (the call site) to the place this method is declared (the declaration site). 

the call site

E.g.,

 

int userInput = IO.readInt(“your int> “);

System.out.println(sumOfWholeNums(userInput));

the declaration site

E.g,

 

public static int sumOfWholeNums(int howFar)

{

}

 

At run time at the call site, the expression inside the parentheses is evaluated to produce a value, then this value is assigned to the corresponding parameter at the method’s declaration site.

 

In the example above, the value held in the memory location named userInput is retrieved and assigned to the variable howFar at the declaration site then the first statement of the sumOfWholeNums() method is called.

More on Parameters

actual – the value supplied to a method by the user – might appear as a literal value or an expression (which might be just the name of a variable) at the call site.

formal – the identifier in the code that takes on the value supplied by the user

 

 

Scope

spatial meaning – the section of code in which a particular variable is ‘visible’.

·         formal parameters are visible and have meaning within the method in whose signature they appear.  We sometimes say they have local scope.  They do NOT have meaning outside the body of the method where they were declared.

·         Constants can be declared outside the body of a method.  We say they have global scope.

 

·         Visibility or spacial meaning is a valid way of thinking at compile-time.

·         The placement of a variable or constant declaration tells the compiler the scope of this variable or constant.

1.    formal parameter declarations appear inside the parenthesis of a method signature and are visible to only the method declaring them.

2.    Constant declarations appear inside a class, but outside of all method signatures.  They are visible to all methods in the class. 

Note: Constant declarations are visible to methods appearing physically before the constant declaration.  For this reason, we usually declare constants at the top of the class body above any method declarations.

 

temporal meaning – at runtime, certain sections of code are active at certain times.  A variable’s scope (in the temporal sense) is when it is valid (or ‘present’ or ‘alive’ or ‘is assigned a particular spot in memory’).

·         constants are present from the time you start the program until it finishes.

·         A method’s formal parameters are present from the time the method is called until the time this method finishes.  A method finishes either by executing its return statement or executing the last statement in the method body.

·         Presence or temporal meaning is a valid way of thinking at run-time.

 

identifiers – how they are treated inside the body of a method

·         The body of a method is the portion appearing between the opening  { and the closing  }.  The names of constants, other methods, and this method’s formal parameters may appear in the body of a method.

·         The name of any variable or method that appears in the body of a method is known as an identifier.

·         The compiler determines whether an identifier is a method name by whether or not the identifier is followed immediately by a (

run-time evaluation of identifiers that are variable names

·         At run-time, the system goes to the memory location named by the variable and reads the value stored in this location.

·         The one exception to this rule is when a variable name appears on the left side of an assignment statement.  In this case, the system writes a value into the memory location named by this variable.