Declarations and Access Controls

In this section you should be able to:

1.      Write code that declares, constructs and initialises arrays of any base type using any of the permitted forms both for declaration and for the initialisation.

2.      Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making use of all permitted modifiers (such as public, final, static, abstract, etc.). State the significance of each of these modifiers both signally and in combination and state the effect of package relationships on declared items qualified by these modifiers.

3.      For a given class, determine if a default constructor will be created and if so, state the prototype of that constructor.

4.      Identify legal return types for any method given the declarations of all related methods in this or parent classes.

 

Declarations and Access Controls

Array Fundamentals

Arrays are used to represent a fixed number of elements of the same type. The following are legal syntax for declaring one-dimensional arrays.

int anArray[];

int[] anArray;

int []anArray;

It is important to note that the size of the array is not included in the declaration. Memory is allocated using the new operator as shown below.

anArray = new int[10];

The declaration and memory allocation may be combined together as shown below.

int anArray[] = new int[10];

The elements of the array are implicitly initialised to default values based on array types (0 for integral types, null for objects etc.). This is true for both local arrays as well as arrays which are data members. In this respect arrays are different from normal variables. Variables defined inside a method are not implicitly initialised, where as array elements are.

 

Array initialisations

Arrays are initialised using the syntax below

int[] intArray = {1,2,3,4};

The length operator can be used to access the number of elements in an array (for example – intArray.length ).

 

Multidimensional Arrays

The following are legal examples of declaration of a two dimensional array,

int[] arr[];

int[][] arr;

int arr[][];

int []arr[];

When creating multi-dimensional arrays the initial index must be created before a later index, the following examples are legal,

int[][] arr = new int[5][5];

int[][] arr = new int[5][];

The following example will not compile

int[][] arr = new int[][5];

 

Class Fundamentals

A class defines a new type and contains methods and variables. The example below demonstrates a simple class,

class Test

{

     String name;            // member variable

 

     public String getName() // method variable

     {

          return name;

     }

 

     public static void main(String[] args)

     {

          int i = 0;         // local variable

     }

} // end of class Test

 

Method Overloading

Java™ allows two methods to have the same name as long as they have different signatures. The signature of a method consists of the name of the method and the amount and type of arguments of the method. Therefore, as long as the argument types of the two methods are different, they may be overloaded (have the same name).

 

Class Constructors

Constructors are member methods that have the same name as the class name. The constructor is invoked using the new operator when a class is created. If a class does not have any constructors then the java language compiler provides an implicit default constructor. The implicit default constructor does not have any arguments and is of the type

Class_name() {}

So in the above class Test, the implicit default constructor would be

Test() {}

If a class defines one or more constructors, an implicit constructor is not provided. The example below gives a compiler error.

class Test2

{

int temp;

 

     Test2(int x)

     {

          temp = x;

     }

 

     public static void main(String[] args)

     {

          Test2 t = new Test2();

 

          /*

This would generate a compilation error

          as there is no constructor without any arguments.

          */

     }

} // end of class Test2

 

Legal Overridden method access

The rules for overriding can be summarised as follows:

·        A private method may be overridden by a private, default, protected, or public method.

·        A default method may be overridden by a default, protected, or public method.

·        A protected method may be overridden by a protected or public method.

·        A public method may only be overridden by a public method.

In simple terms, methods may not be overridden to be more private.

 

Summary of Access Modes

public

A public feature may be accessed by any class.

protected

A protected feature may only be accessed by a subclass of the class that owns the feature or by a member of the same package as the class that owns the feature.

default

A default feature may only be accessed by a class from the same package as the class that owns the feature.

private

A private feature may only be accessed by the class that owns the feature.

 

 

All possible combinations of Features and Modifiers

Modifier

Class

Variable

Method

Constructor

Free – Floating Block

public

yes

yes

yes

yes

no

protected

no

yes

yes

yes

no

(default)*

yes

yes

yes

yes

yes

private

no

yes

yes

yes

no

final

yes

yes

yes

no

no

abstract

yes

no

yes

no

no

static

no

yes

yes

no

yes

native

no

no

yes

no

no

transient

no

yes

no

no

no

volatile

no

yes

no

no

no

synchronized

no

no

yes

no

yes

* Default is not a modifier; it is just the name of the access if no modifier is specified.

Points to remember:

·        A final class cannot be subclassed; a final variable cannot be modified after initialization; a final method cannot be overridden.

·        An abstract class cannot be instantiated; an abstract method’s definition is deferred to a subclass.

·        Static variables belong to the class; static methods have no this pointer and may not access non-static variables and methods of their class.

·        A static feature may be referenced through the class name or through a reference to any instance of the class.

·        Static initializer code appears in curly brackets “{ }” with no method declaration. Such code is executed once, when the class is loaded.

 

back