Saturday, April 01, 2006

Declarations and Access Control

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

Declaring arrays:

For example,use either

 int[] x;


 int x[];

Both are legal positions for the brackets. To declare a multidimensional array, use multiple sets of brackets, e.g. int i[][]; declares a two dimensional array. Note that, for example, int[]i[]; is also legal, and means the same thing. In Java, multidimensional arrays can be "not square" i.e. they are just arrays of arrays, and each of those constituent arrays can be of a different size.

Construct arrays:

Arrays are objects. Use the new keyword to construct them. For example having declared an array i:

int[] i;

you then construct the array and assign it to i as follows:

i = new int[10];

The size of the array is in the brackets (in this example 10). It is common to do both operations in one statement:

int i[] = new int[10];

To initialize an array using loop iteration:

An example:

 int array[] = new int[15];
 for(int j=0; j

Write code to initialize an array using the combined declaration and initialization format:

An example:

char c[]= new char[] {'a','b','c','d','e'};

or you can use

char c[]= {'a','b','c','d','e'};
Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

This is fundamental Java. If you are unsure of this topic, see a textbook.

A good but technical reference for these kinds of issues is the Java Language Specification (not for beginners). It is available at the following URL:

Some terms and their synonyms:

Scope/Visibility: Where something can be seen / is accessable from.

Nested/Inner Class: A class whose code sits inside the body of another 'outer' class. It exists 'inside' the class in that it can see the private methods and variables.

Instance/Member: Means the method/variable/nested class belongs to each object which is an instance of the class. The term 'a member' is often used to refer to both methods and variables of this type. Cannot be accessed by statically i.e. without having an instance of the class.

Class/Static: The method/variable/nested class belongs to the class as opposed to the instances of the class. Can be used without creating an instance of the class, but static methods/nested class cannot use instancts variables/methods.

Local/Automatic variable: A variable which is declared within a method or as a parameter to the method. Cannot be seen outside the method.

Scoping Types

"default" or "friendly" - this is where no modifier is used. Means something is only visible to classes in the same package.

protected - can only be accessed by classes in the same package or subclasses of this class. This frequently surprises experienced developers, especially those with a prior backround in the mutant hybrid of object orientated programming and assembly language known as 'C++' :-) To fair the name is misleading, as it sounds like it restricts access, where as it in fact it adds subclasses outside the package to the list of things that can access the item in question, as compared to using "default" access.

public - can be accessed by any other class.

private - can only be accessed from inside the class.

Declare classes using the modifiers public, abstract or final:

public - is visible outside of its package. Without this modifier, the class cannot be accessed outside its package.

abstract - cannot be instantiated, is allowed to contain abstract methods.

final - cannot be subsclassed.

Using the modifiers private, protected, public, static, final, native or abstract:

private - can only be accessed from inside the class. Private members are not inherited by subclasses. Inner classes can be declared private.

protected - can only be accessed by classes in the same package or subclasses of this class.

public - can be accessed by any other class.

static - belongs to the class rather than any particular instance of the class. For variables, effectively, there is just one copy of this variable for all instances of the class, and if an instance changes the value, the other instances see that new value. For methods, it means the method can be called without having created an instance, but within the methodd you cannot use the this keyword, or refer to instance variables and methods directly (without creating an instance and referring to the variable/class in that instance). For inner classes, it means they can be instantiated without having an instance of the enclosing class, but as with static methods, the methods of the inner class cannot refer to instance variables or methods of the enclosing class directly.

final - cannot be changed. Variables are constants, methods cannot be overridden, classes cannot be subclassed. Since Java1.1 you can declare the variable without assigning a value. Once you assign a value, you cannot change it. The are known as 'blank' finals, are frequently used for things like constants you wish to initialise from a configuration file.

native - a method which is not written in java and is outside the JVM in a library.

abstract - a method which is not implemented. Must be implemented in a subclass if that subclass is to be 'concrete' and allow people to instantiate it.

Nested Classes

To define a non-static nested class either in a class or method scope:

Place the class definition (for the nested class) inside another class definition (the outer class) or a method.

To define, in method scope, an anonymous nested class that implements a specified interface:

An anonymous nested class is defined where is it instantiated (in a method). An anonymous nested class must either implement an interface or extend a class, but the implements or extends keywords are not used. For example the following line causes the method to return an object which is an instance of an anonymous nested class:

return new SomeClass() { /*body of the anonymous class goes here*/ };

You might like to think of an anonymous nested class as part of a really long new statement, which happens to contain a class definition, and which is why it has a ";" at the end. The following example calls someMethod(), passing an instance of the anonymous nested class:

someMethod(new SomeClass() { /*body of the anonymous class goes here*/ });

In both cases SomeClass() is not the name of the anonymous class (anonymous means it has no name) rather is it the name of the class that you are extending or the interface you are implementing. These classes cannot define a constructor, as they do not have a name that you can use to declare the constructor method. If SomeClass() is a class, the default constructor of that class is called, if you want to use a non-default constructor instead, you supply arguments e.g.:

return new SomeClass(12) { /*body of the anonymous class goes here*/ };

will call the SomeClass constructor which takes an int.

Write code in a non-static method of the outer class to construct an instance of the nested class.

Inner x = new Inner(); constructs an instance of Inner where Inner is a nested class defined in the current class.

Write code to construct an instance on a nested class where either no this object exists, or the current this object is not an instance of the outer class.

You must create an instance of the outer class first. Given a class, Outer, containing a nested class Inner:
Outer.Inner y = new Outer().new Inner();

The above creates an instance of Inner called y, but it had to construct an instance of Outer first. The following example creates the Outer instance on a separate line, the syntax in the second line is the one you use when you already have an instance of the outer class.

Outer x = new Outer();
Outer.Inner y = Inner();
If Inner is static, you can use:
Outer.Inner I= new Outer.Inner();

State which variables and methods in enclosing scopes are accessible from methods of the inner class.

A non-static inner class has access to all member variables and methods of the containing class. If the inner class is defined inside a method, it has access to those method (a.k.a. automatic or local) variables which are declared final, in addition to the above.

A static inner class is restricted in the same way as a static method: it cannot refer to instance variables and methods of the containing class directly (without creating an instance and referring to the variable/class in that instance).

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

The default constructor takes no arguments e.g. classname() where classname is the name of you class. A default constructor is automatically created only if you do not create any constructors in your class. If you create a non-default constructor (i.e. one that takes an argument), then you may have to create a default one yourself, if you want there to be one.

All constructors call the default constructor of its parents class (if there is one), and so on up the hierarchy to Object, unless you specify a different constructor using super(...) (to use a constructor in the parent) or this(...). If you use such calls, they must be the first thing in the constructor.

©1999, 2000, 2002 Dylan Walsh. Under free documentation liscence.