A (175)

(Tuis.) #1
CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces 153

Java methods have their own rules regarding inheriting access control modifiers as classes that
contain them are later subclassed and enhanced to become more detailed and refined subclasses.
For instance, any method that has been declared using public access in a superclass must also be
declared using public access in all subclasses. Similarly, any method that has been declared using
protected access in a superclass must either be declared using protected access, or using public
access, in any subclass. It can never be declared using a private access control modifier. A method
declared without using an access control modifier is the only scenario where a method can be
declared using a private access control modifier in a subclass.


It is important to note that a method that has been declared using a private access control modifier
keyword is not inherited, because it is private relative to the class within which it is contained, and
no others, including any subclasses. As you can see, although access control modifiers seem fairly
simple and straightforward, you have to pay attention to what you are doing with them, especially
where inheritance (superclasses and subclasses) is going to be utilized in your Java programming
structure and package design.


Non-Access Modifiers: Static, Final & Abstract


There are also modifiers in Java that are not access control modifiers and not data type declarations.
These are called non-access modifiers, and these are the most complicated ones to understand and
implement in practical use. There are three modifiers that are frequently used in Java programming
that we will be covering in this section of the chapter: the static modifier, the final modifier, and the
abstract modifier.


There are also some more advanced modifiers such as synchronized or volatile, which are used to
manage the use of threads, a topic that is beyond the scope of an Absolute Beginners level Android
programming book.


The static Keyword


A static modifier keyword when used in conjunction with a variable will create a variable that
will exist independently of any object instances created using that class. Static variables will be
initialized only one time, at the start of the execution of the application, sometimes called the
“app launch.” The variables that use the static modifier keyword will be initialized first, before the
initialization of any instance variables.


Only one copy of a static variable will exist in system memory regardless of the number of instances
of the class that contains that variable are created. Thus, static in Java programming means a
variable that belongs to the class and not to the object instances created by that class.


Objects created by that class can share that variable with the class and with each other, so use of
static variables can optimize system memory. The opposite of static is “dynamic,” and thus, any
variable not declared as static would therefore be “dynamically” created (created at the time it is
needed, not ahead of time as when it is declared statically), and system memory would be allocated
for that variable in each object instance created by the class constructor method.

Free download pdf