Site hosted by Angelfire.com: Build your free website today!
scjp section 1: declarations and access control

return to computer science and java

Section 1: Declarations and Access Control

Section 2: Flow Control, Assertions, and Exception Handling

Section 3: Garbage Collection

Section 4: Language Fundamentals

Section 5: Operators and Assignments

Section 6: Overloading, Overriding, Runtime Type and Object Orientation

Section 7: Threads

Section 8: Fundamental Classes in the JAVA.LANG Package

Section 9: The Collections Framework

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

Arrays

  1. Declaration
    • of an array of primitives, or object references
    • to the compiler
    • int[] ns; is equivalent to int ns[];, Object[] objects; foat[][] twoD;
    • also, a method that returns an array can do so:
      like this: returnarraytype methodName()[] { ...
      or this: returnarraytype[] methodName() { ...

  2. Construction
    • at run-time (therefore can specify size as a variable)
    • can combine with declaration eg. int[] ns = new ints[10];

  3. Initialization
    • automatic: boolean → false, numbers → 0 (or 0.0, 0.0f etc), char → '\u0000' and objects → null.
    • can combine declaration/construction/initialization:
      eg. int[] ns = {1, 2, 4, 8, 16};
      which is equivalent to: int[] ns = int[]{1, 2, 4, 8, 16};
      or by: ns[0] = 1; ns[1] = 2; etc.

  4. nameofarray.length
  5. indices start at 0.

  • 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.

Modifiers

  1. order of modifiers is irrelevant
  2. e.g. public final is equivalent to final public

  3. Access Modifiers
    • determine which class(es) can use a feature (class, instance, variable, methods, constructors).
    • public

      • main methods and applets must be declared public
      • all top-level (ie. on-inner) classes must be declared public or have default access
    • protected

      • only variables and methods can be declared protected.
      • available to all classes in the same package, and all sub-classes.
      • overrides a classes access.
      • a top-level class can't be declared protected.
    • default

      • if none given e.g. class MyClass {..., void myMethod() { ...
      • available to any class in the same package
      • if no package set, Java assumes all .class files in a directory to be part of the same package
      • not available to sub-classes outside the package.
    • private

      • can only be used by the class that defines it so even sub-classes can't access these methods/variables directly.
      • a top-level class can't be declared private.
    • subclasses and method privacy

      • methods can't be overridden to become more private (i.e. subclasses can move up the above list, to become more public, but not down).

  4. final
    • a final class
      • can't be subclassed
      • eg. java.lang.Math is final so can't have NewMath extends Math
    • a final variable
      • can't be modified once assigned a value, e.g. Math.PI
      • primitive or a reference to an object (object itself can be modified).
    • a final method
      • can't be overridden
      • note: a constructor can't be declared final (as they are not inherited anyway - so no such thing as overriding them)

  5. abstract
    • variables can't be declared abstract
    • an abstract class
      • cannot be instantiated (i.e. can't call constructor)
      • must be sub-classed to use (implementation is deferred to sub-classes)
      • a class must be declared abstract if:
        • it has one or more abstract methods
        • it inherits one or more abstract methods that it doesn't implement
        • it declares it implements an interface but doesn't implement all it's methods
      • can contain final methods
    • an abstract method
      • no implementation (end with ';').
      • a subclass must implement all abstract methods or be declared abstract
      • constructors can't be declared abstract

  6. static
  7. methods and variables
    • belong to AClass rather than an instance of one
    • brought in once, and only once, when AClass is loaded
    • can be used to count the number of instances of a class (eg. static num = 0; then in constructor num++;)
    • can be accessed by AClass, or instanceOfAClass (latter considered bad form but legal)
    • static methods can't use non-static features of it's class (i.e. this is undefinded in static methods)
    • static initializers - a block of code outside any method, and is initialized just once on loading of the method
    • static methods cannot be overriden to become non-static
    • static members can only be declared in top-;evel or static types

  8. native methods
    • body of method is to be found outside the JVM. amd written in a non-java language (eg. C/C++) library.
      eg. native void theOutsideMethod(arguments eg. int i);
      then to ensure the library will be available whenever needed:
          static {
              System.loadLibrary("LibraryContainingMethod")
          }
    • calls to native methods don't have to know that the method is native eg. classInstance.theOutsideMethod(10);
    • clone() and notify() of Object class are native.

  9. transient variables
    • not stored as part of the objects persistant state
    • used for sensitive information in objects that get written somewhere outside th JVM (such as objects that implement Serializable or Externalizable interfces).

  10. synchronized
    • - see Section 7: Threads
    • methods and free-floating blocks of code only

  11. volatile variables
    • can be modified asynchronously

  12. Constructors
    • can't be declared final, abstract, static, native or synchronized
    • may have any access modifier (public|protected|default|private)
    • a sub-classes constructor implicitly calls it's superclasses constructor first, before executing it's own code

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

  • a no-argument constructor is created by the compiler in the absence of any defined constructor.

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

  • overriding methods must have the same return type as the original method. (overloaded methods can have whatever return type they like)