SCJP Java Programming Exam Cram Notes : Modifiers

3. final

  • final features cannot be changed.
  • final classes cannot be sub-classed.
  • final variables cannot be changed. (Either a value has to be specified at declaration or an assignment statement can appear only once).
  • final methods cannot be overridden.
  • Method arguments marked final are read-only. Compiler error, if trying to assign values to final arguments inside the method.
  • Member variables marked final are not initialized by default. They have to be explicitly assigned a value at declaration or in an initializer block. Static finals must be assigned to a value in a static initializer block, instance finals must be assigned a value in an instance initializer or in every constructor. Otherwise the compiler will complain.
  • Final variables that are not assigned a value at the declaration and method arguments that are marked final are called blank final variables. They can be assigned a value at most once.
  • Local variables can be declared final as well.

4. abstract

  • Can be applied to classes and methods.
  • For deferring implementation to sub-classes.
  • Opposite of final, final can't be sub-classed, abstract must be sub-classed.
  • A class should be declared abstract,
    • if it has any abstract methods.
    • if it doesn't provide implementation to any of the abstract methods it inherited
    • if it doesn't provide implementation to any of the methods in an interface that it says implementing.
  • Just terminate the abstract method signature with a ';', curly braces will give a compiler error.
  • A class can be abstract even if it doesn't have any abstract methods.

5. static

  • Can be applied to nested classes, methods, variables, free floating code-block (static initializer)
  • Static variables are initialized at class load time. A class has only one copy of these variables.
  • Static methods can access only static variables. (They have no this)
  • Access by class name is a recommended way to access static methods/variables.
  • Static initializer code is run at class load time.
  • Static methods may not be overridden to be non-static.
  • Non-static methods may not be overridden to be static.
  • Abstract methods may not be static.
  • Local variables cannot be declared as static.
  • Actually, static methods are not participating in the usual overriding mechanism of invoking the methods based on the class of the object at runtime. Static method binding is done at compile time, so the method to be invoked is determined by the type of reference variable rather than the actual type of the object it holds at runtime.
  • Let's say a sub-class has a static method which 'overrides' a static method in a parent class. If you have a reference variable of parent class type and you assign a child class object to that variable and invoke the static method, the method invoked will be the parent class method, not the child class method.

The following code explains this.

public class StaticOverridingTest {

public static void main(String s[]) {
Child c = new Child();

c.doStuff(); // This will invoke Child.doStuff()

Parent p = new Parent();

p.doStuff(); // This will invoke Parent.doStuff()

p = c;

p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff() 

}

}

class Parent {

static int x = 100;

public static void doStuff() {

System.out.println("In Parent..doStuff");

System.out.println(x);

}

}

class Child extends Parent {

static int x = 200;

public static void doStuff() {

System.out.println("In Child..doStuff");

System.out.println(x);

}

}
						

6. native

  • Can be applied to methods only. (static methods also)
  • Written in a non-Java language, compiled for a single machine target type.
  • Java classes use lot of native methods for performance and for accessing hardware Java is not aware of.
  • Native method signature should be terminated by a ';', curly braces will provide a compiler error.
  • native doesn't affect access qualifiers. Native methods can be private.
  • Can pass/return Java objects from native methods.
  • System.loadLibrary is used in static initializer code to load native libraries. If the library is not loaded when the static method is called, an UnsatisfiedLinkError is thrown.

7. transient

  • Can be applied to class level variables only.(Local variables cannot be declared transient)
  • Transient variables may not be final or static.(But compiler allows the declaration, since it doesn't do any harm. Variables marked transient are never serialized. Static variables are not serialized anyway.)
  • Not stored as part of object's persistent state, i.e. not written out during serialization.
  • Can be used for security.

8. synchronized

  • Can be used for security.
  • Used to control access to critical code in multi-threaded programs.

9. volatile

  • Can be applied to variables only.
  • Can be applied to static variables.
  • Cannot be applied to final variables.
  • Declaring a variable volatile indicates that it might be modified asynchronously, so that all threads will get the correct value of the variable.
  • Used in multi-processor environments.
Modifier Class Inner classes (Except local and anonymous classes) Variable Method Constructor Free floating Code block
public Y Y Y Y Y N
protected N Y Y Y Y N
(friendly)

No access modifier

Y Y (OK for all) Y Y Y N
private N Y Y Y Y N
final Y Y (Except anonymous classes) Y Y N N
abstract Y Y (Except anonymous classes) N Y N N
static N Y Y Y N Y (static initializer)
native N N N Y N N
transient N N Y N N N
synchronized N N N Y N Y (part of method, also need to specify an object on which a lock should be obtained)
volatile N N Y N N N

Previous    Contents    Next