Java Array Modifiers
Java Array Modifiers
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. . You must include the size of an array when you construct it (using new) unless you are creating an anonymous array. . Elements in an array of objects are not automatically created, although primitive array elements are given default values. . Youll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object. . Arrays are indexed beginning with zero. In an array with three elements, you can access element 0, element 1, and element 2. . Youll get an ArrayIndexOutOfBoundsException if you try to access outside the range of an array. . Arrays have a length variable that contains the number of elements in the array. . The last index you can access is always one less than the length of the array. . Multidimensional arrays are just arrays of arrays. . The dimensions in a multidimensional array can have different lengths. . An array of primitives can accept any value that can be promoted implicitly to the declared type of the array. For example, a byte variable can be placed in an int array. . An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array. . If you assign an array to a previously declared array reference, the array youre assigning must be the same dimension as the reference youre assigning it to. . You can assign an array of one type to a previously declared array reference of one of its supertypes. For example, a Honda array can be assigned to an array declared as type Car (assuming Honda extends Car).
Using a Variable or Array Element That Is Uninitialized and Unassigned . When an array of objects is instantiated, objects within the array are not
instantiated automatically, but all the references get the default value of null. . When an array of primitives is instantiated, all elements get their default values. . Just as with array elements, instance variables are always initialized with a default value. . Local/automatic/method variables are never given a default value. If you attempt to use one before initializing it, youll get a compiler error.
Command-Line Arguments to Main . Command-line arguments are passed to the String array parameter in the
main method. . The first command-line argument is the first element in the main String array parameter. . If no arguments are passed to main, the length of the main String array parameter will be zero.
Class Access Modifiers . 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. . 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 . 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 Modifiers (nonaccess) . 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
abstract methods.
Member Access Modifiers . 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 different packages. . If a superclass member is public, the subclass inherits itregardless 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. . Default and protected members differ only in when subclasses are involved: . Default members can be accessed only by other classes in the same package. . Protected members can be accessed by other classes in the same package, plus subclasses regardless of package. . Protected = package plus kids (kids meaning subclasses). . For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to an instance of the superclass (in other words, inheritance is the only mechanism for a subclass outside the
package to access a protected member of its superclass). . A protected member inherited by a subclass from another package is, in practice, private to all other classes (in other words, no other classes from the subclass package or any other package will have access to the protected member from the subclass).
Local Variables . Local (method, automatic, stack) variable declarations cannot have access
modifiers. . final is the only modifier available to local variables. . Local variables dont get default values, so they must be initialized before use.
Other ModifiersMembers . Final methods cannot be overridden in a subclass. . Abstract methods have been declared, with a signature and return type, but
have not been implemented. . Abstract methods end in a semicolonno curly braces. . Three ways to spot a nonabstract method: . The method is not marked abstract. . The method has curly braces. . The method has code between the curly braces. . The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class abstract methods. . Abstract methods must be implemented by a subclass, so they must be inheritable. For that reason: . Abstract methods cannot be private. . Abstract methods cannot be final. . The synchronized modifier applies only to methods. . Synchronized methods can have any access control and can also be marked final. . Synchronized methods cannot be abstract. . The native modifier applies only to methods. . The strictfp modifier applies only to classes and methods. . Instance variables can . Have any access control . Be marked final or transient . Instance variables cannot be declared 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 have the following properties: . 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.
. They are not tied to any particular instance of a class. . An instance of a class does not need to exist in order to use static members of
the class. . There is only one copy of a static variable per class and all instances share it. . Static variables get the same default values as instance variables. . A static method (such as main()) cannot access a nonstatic (instance) variable. . Static members are accessed using the class name: ClassName.theStaticMethodName() . Static members can also be accessed using an instance reference variable, someObj.theStaticMethodName() but thats just a syntax trick; the static method wont know anything about the instance referred to by the variable used to invoke the method. The compiler uses the class type of the reference variable to determine which static method to invoke. . Static methods cannot be overridden, although they can be redeclared/ redefined by a subclass. So although static methods can sometimes appear to be overridden, polymorphism will not apply (more on this in Chapter 5).