Pages

Jul 5, 2012

Declaration and Access Control


Identifiers (Objective 1.3)

  •  Identifiers can begin with a letter, an underscore, or a currency character.
  •  After the first character, identifiers can also include digits.
  •  Identifiers can be of any length.
  •  JavaBeans methods must be named using camelCase, and depending on the method's purpose, must start with set, get, is, add, or remove.

Declaration Rules (Objective 1.1)

  • A source code file can have only one public class.
  • If the source file contains a public class, the filename must match the public class name.
  • A file can have only one package statement, but multiple imports.
  • The package statement (if any) must be the first (non-comment) line in a source file.
  • The import statements (if any) must come after the package and before the class declaration.
  • If there is no package statement, import statements must be the first (noncomment) statements in the source file.
  • package and import statements apply to all classes in the file.
  • A file can have more than one nonpublic class.
  • Files with no public classes have no naming restrictions.

Class Access Modifiers (Objective 1.1)

  • There are three access modifiers: public, protected, and private.
  • There are four access levels: public, protected, default, and private.
  • Classes can have only public or default access.
  • A class with default access can be seen only by classes within the same package.
  • A class with public access can be seen by all classes from all packages.
  • Class visibility revolves around whether code in one class can
    • Create an instance of another class
    • Extend (or subclass), another class
    • Access methods and variables of another class

Class Modifiers (Nonaccess) (Objective 1.2)

  • Classes can also be modified with final, abstract, or strictfp.
  • A class cannot be both final and abstract.
  • A final class cannot be subclassed.
  • An abstract class cannot be instantiated.
  • A single abstract method in a class means the whole class must be abstract.
  • An abstract class can have both abstract and nonabstract methods.
  • The first concrete class to extend an abstract class must implement all of its abstract methods.

 Member Access Modifiers (Objectives 1.3 and 1.4)

  • Methods and instance (nonlocal) variables are known as "members."
  • Members can use all four access levels: public, protected, default, private.
  • Member access comes in two forms:
    • Code in one class can access a member of another class.
    • A subclass can inherit a member of its superclass.
  • If a class cannot be accessed, its members cannot be accessed.
  • Determine class visibility before determining member visibility.
  • public members can be accessed by all other classes, even in other packages.
  • If a superclass member is public, the subclass inherits it—regardless of package.
  • Members accessed without the dot operator (.) must belong to the same class.
  • this. always refers to the currently executing object.
  • this.aMethod() is the same as just invoking aMethod().
  • private members can be accessed only by code in the same class.
  • private members are not visible to subclasses, so private members cannot be inherited.



Static Variables and Methods (Objective 1.4)

  • 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 (Objective 1.3)

  • An enum specifies a list of constant values assigned to a type.
  • An enum is NOT a String or an int; an enum constant's type is the enum type. For example, SUMMER and FALL are of the enum type Season.
  • 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.
  • Enums can contain constructors, methods, variables, and constant class bodies.
  • enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor.
  • enum constructors can have arguments, and can be overloaded.
  • 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. These are legal:
  • enum Foo { ONE, TWO, THREE}
  • enum Foo { ONE, TWO, THREE};
  • MyEnum.values() returns an array of MyEnum's values.

Methods with var-args (Objective 1.4)

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




No comments:

Post a Comment