26 February 2008

Declarations and Access Control (Part III)

Other Modifiers—Members :

  • final methods cannot be overridden in a subclass.
  • abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not implemented.
  • The synchronized modifier applies only to methods and code blocks.
  • synchronized methods can have any access control and can also be marked final.
  • The native modifier applies only to methods.
  • The strictfp modifier applies only to classes and methods.
Methods with var-args :
  • As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method.
  • A var-arg parameter is declared with the syntax type... name; for instance : doStuff(int... x) { }.
  • A var-arg method can have only one var-arg parameter.
  • In methods with normal parameters and a var-arg, the var-arg must come last.
Variable Declarations :
  • Instance variables can have any access control and be marked final or transient.
  • Instance variables can't be abstract, synchronized, native, or strictfp.
  • It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing."
  • final variables cannot be reinitialized once assigned a value.
  • final reference variables cannot refer to a different object once the object has been assigned to the final variable.
  • final reference variables must be initialized before the constructor completes.
  • There is no such thing as a final object. An object reference marked final does not mean the object itself is immutable.
  • The transient modifier applies only to instance variables.
  • The volatile modifier applies only to instance variables.
Array Declarations :
  • Arrays can hold primitives or objects, but the array itself is always an object.
  • When you declare an array, the brackets can be to the left or right of the variable name.
  • It is never legal to include the size of an array in the declaration.
Static Variables and Methods :
  • They are not tied to any particular instance of a class.
  • No classes instances are needed in order to use static members of the class.
  • There is only one copy of a static variable / class and all instances share it.
  • static methods do not have direct access to non-static members.
Enums :
  • An enum specifies a list of constant values that can be assigned to a particular type.
  • An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.
  • enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.
  • The semicolon at the end of an enum declaration is optional.

No comments: