Tuesday, April 10, 2007

NAMES

NAMES

Introduction: names are used to refer to entities declared in a program. Declared entities are package, class type (normal or enum type), interface type (normal or annotation type), member (class, interface, fields or methods) of a reference type, type parameters (of a class, interface, method or constructor), parameter (to a method, constructor or exception handler) or local variable.

This chapter discusses on types of names, scope of the declaration that introduces a name, members of entities such as package and reference types, and access control modifiers for the members of named entities.

The uses and meanings of names are also discussed.

Declaration: (these section should be retained as referential; most of the entities have already been discussed in blogs before this). A declaration introduces an entity into a program and it includes an identifier that can be used in a name to refer to this entity. A declared entity is one of the following:

1. a package, declared in a package declaration
2. an imported type, declared in a single-type-import declaration or a type-import-on-demand declaration.
3. a class, declared in a class type declaration.
4. a type variable, declared in a formal type parameter of a generic class, interface, method or constructor.
5. a member of a reference type, one of the following:
- a member class
- a member interface
- an enum constant
- a field, one of the following,
o a field declared in a class type
o a constant field declared in an interface type
o the field length which is implicitly a member of every array type
- a method, one of the following,
o a method (abstract or others) declared in a class type
o a method (always abstract) declared in an interface type
6. a parameter, one of the following,
- a parameter of a method or constructor of a class.
- a parameter of an abstract method of an interface
- a parameter of an exception handler declared in a catch clause of a try statement.
7. a local variable, one of the following,
- a local variable declared in a block
- a local variable declared in a for statement.

Note: constructors are also introduced by declarations, but use the name of the class in which they are declared rather than introducing a new name.

Names and identifiers:

First take it to heart that every name is an identifier but not all identifiers are names.
Where an identifier is also a name:
1. when the reference is to an entity declared in a program. These names can be simple names i.e single identifiers, or qualified names i.e a name with a ‘.’ token followed by an identifier.
Rules for determining the meaning of names will be in a later blog.
Where reference is not to an entity, then an identifier is not a name. identifiers that are not names are:
a. field acess expressions.
b. Method invocation expressions
c. In qualified class instance creation expressions.
d. As labels in labeled statements and in break and continue statements that refer to statement labels.

package api_package;

//we'll use a simple example here
//Loader is a name, refers to an entity
public class Loader {
String name; //name is also a name, entity variable

Loader(String givenname){
this.name=givenname;
}

int ageCounter(int anynumber){ //listPosition is a name
return anynumber*2;
}
public static void main(String[] args) {
//create an instance of Loader
//the qualified class instance creation expression
//is an identifier
Loader ourman = new api_package.Loader("Koro");

//method invocation expression is an identifier
int guessedage = ourman.ageCounter(15);

//we'll guess Koro's age
//the field access expression is identifier
System.out.print("This man "+ourman.name); //field access
System.out.print(" is really unbelievable for someone");
System.out.println(" above "+guessedage+" years.");
}
}

Scope of a declaration: the scope of a declaration is the region of a program within which the entity declared by the declaration can be referred to using a simple name(provided it is visible). A declaration is in scope at a particular point in a program if and only if the declaration’s scope includes that point.

Examples:
//api_package package is declared here, scope starts here
//and is observable here. scope ends on completion of every
//compilation unit in this package ends
package api_package;

//import declaration here, scope starts here, global type import
import java.io.Serializable;

public class Loader {
//scope of String type starts with its declaration here
//member scopesearcher has scope that is global, i.e in the
//entire body of class Loader
String scopesearcher;

//scope of parameter, expression is confined to the
//body of the method, setSearcher
void setSearcher(String expression){
try {
scopesearcher = expression;
//scope of myindex, local variable, starts from here
//to the end of this block
Integer myindex = 2;

//scope of n, exception handler parameter, is the entire catch clause block.
}catch (NumberFormatException n) {

}
}

//scope of type parameter, T in matchCase
//is the entire declaration of the method, and the
//type parameter's section itself.
void matchCase(T givennumber){

//scope of Forinit part, i.e i, includes the statements in
//parenthesis and the statements enclosed in the block.
//ends on block’s end therefore i can still be used
//outside the for statement.
for (int i=0; i<34; i++){
System.out.println(i);
}
}

//scope of HelpLoad, a local class, starts with its declaration
//to the end of the immediately onclosed block.
class HelpLoad{

}

public static void main(String[] args) {
}
}

No comments: