Names – Java Specification chapter six
Comments: 1Today I cover chapter six of Java Specification. The chapter is about names. Names are used to refer to entities in our program. Name can be : (package, class type, interface type, field, method, reference type, type parameter, constructor, exception handler or local variable.
- Name can be simple (without dot) and qualified (with dot)
- Every declaration has scope, which means that the ‘name’ is recognized in this scope (simple name).
- In determining the ‘name’ meaning we use context of the occurrence, and than we know if the name is package, type, variable or method (can use the same name). See example below.
- Access control can be specified. Access is different concept than scope. Access defines qualified access to the ‘name’
- Declaration introduce an entity into a program and also may introduce a name. Note that constructor use the name of the class in which they are declared, so it doesn’t introduce a name.
- Not all identifiers are part of the name. They can be used also as : in declarations, field access expression, method invocation expression, qualified class instance creation, as label in label statement (also break and continue). For example label can have the same name as variable.
- Scope of declaration is the region of the program when we can refer to entity by simple name.
- scope of parameter is the entire body of method or constructor
- scope of local variable in a block is the rest of the block starting with its initializer.
- scope of local variable declared in for statement includes (initializer, any further declaration to the right, expression and code part and contained statement.
- scope of a parameter of an exception handler is the entire block associated with catch statement.
- Declaration may be shadowed by another declaration of the same name. Shadowing is different from hiding and obscuring
- Obscured declaration may occur in context when we cannot refer to a visible type or package via its simple name. A simple example when we have two classes with the same name.
- No two distinct members of the same package may have the same name, but members of different packages may.
- Warning: Class or interface may have two or more fields with the same name if they are declared in different interfaces and inherited. See example below.
- Method is overloaded if the method has signature that is not override equivalent.
- If the method is abstract than new declaration is said to implement it, otherwise its override it.
- Interface gets all method signatures from Object (if it don’t declare it), but it can not override final Object methods.
- Array members are
- public final field length
- public method clone, the return type of clone is T[]
- all members from Object class without clone method.
- There is a paragraph (6.5) about name meaning, the algorithm of determining this has three steps.
- context causes a name to fall into one of six categories: ( PackageName, TypeName, ExpressionName, MethodName, PackageOrTypeName or AmbiguousName)
- name classified as PackageOrTypeName or AmbiguousName is reclassified to be PackageName, TypeName, ExpressionName, MethodName.
- the resulting category determines the meaning of the name (or compilation time error if the name has no meaning).
- Algorithm is many pages long and it has nothing interesting for real developers, so I omitted it here.
- One note is that names may be qualified by type names, but not by types in general so that is not possible to access a class variable through a parameterized type. The same rule applies to methods see example:
- Accessibility is a static property that can be determined at compile time.
- Package is always accessible
- Public class or interface may be accessed by any code.
- All members of interfaces are implicitly public !!!.
- Public members are accessible, protected members are accessible then they are accessible for package and outside package only for the implementation of that object., private members are accessible only for the body of the top level class, otherwise we say there is default access which is accessible only from package in which the type is declared.
- Some tricky examples:
- FQN means Fully Qualified Name, for primitives it is just the same (eg. for long is “long”) for reference types is name with package (eg for Object it is java.lang.Object).
- Last paragraph ($6.8) is about convention, but as we know every one has its own the best one.
Comments
kretes
one great job, more, keep it up and I really think that aggregation of all Your articles on that topic are of good value for many java developers!