1.1 OCA 1Z0-808 Module01 Java Basics Review
1.1 OCA 1Z0-808 Module01 Java Basics Review
JAVA BASICS –
REVIEW
ABBREVIATIONS & ACRONYMS
4
SCOPE OF VARIABLES
Variables can operate on different levels they can have multiple scopes:
class-level variables,
instance variables,
local variables (including loop vars), and
method arguments.
Local vars are most often defined within the body of a method/ctor and in sub-blocks.
The scope of a variable is limited by the nearest pair of matching curly braces, {}. For
example, the scope of a local variable is less than the scope of a method if the variable
was declared within a sub-block inside the method. This sub-block can be:
• if statement
• switch construct
• any loop
• TCF construct
• just a group of assorted stats enclosed by a matching pair of braces.
5
SCOPE OF VARIABLES, cont’d
Local variables are not visible (read: they cannot be accessed) outside the method or
sub-block in which they’re defined.
Instance variables are defined and accessible within an object, which effectively means
we need a valid object to work with them. Any instance method of the same class can
access these instance variables.
Class-level variables, a.k.a. static variables, are shared by all of the instances of the
class; what’s more, they can be accessed even if there are no objects of the class.
Method arguments are used by a method that accepts parameters. Their scope is
confined to the method within which they’re defined.
A method argument and a local variable cannot share the same identifier, that is, name.
Same goes for class and instance variables: they can’t be defined using the same name.
6
SCOPE OF VARIABLES, cont’d
On the other hand, local and class/instance vars may be defined using the same name. If
a method defines a local var that shares the same name with an class/instance variable,
the local var shadows the class/instance var (in other words, makes it invisible.)
Sometimes the variables’ scopes overlap with each other; the only way to be sure if a
variable is accessible is to identify the block it belongs to (by looking at braces).
Loop variables are local to the loop within which they’re defined.
Local variables go out of scope when the block they are declared in ends.
7
SCOPE OF VARIABLES, cont’d
1 class VarScope{
2 static int x = 4, y;
3 static{
4 x = 44;
5 }
6 int a = 1, b;
7 {
8 b = 11;
9 }
10 void run(int b){
11 int a = b;
12 int c;
13 {
14 // int c = 666; // INVALID
15 int x = 444;
16 }
17 for (int d = 0; d < 3; d++){
18 // int a = 3; // INVALID
19 int e = 5;
20 e++;
21 System.out.println("e = " + e); // prints 6 repeatedly
22 }
23 }
24 public static void main(String[] args) {
25 int a = 3;
26 new VarScope().run(a);
27 }
28 } 8
1.2
JAVA CLASS
STRUCTURE
9
JAVA CLASS STRUCTURE
The structure and functions of Java class are defined in a source code file (.java file).
The compiler creates a single Java bytecode file (.class file) for each compiled class,
even if it is nested. As for the .class files themselves, they are not on the 1Z0-808 exam.
A class can define multiple components, such as:
• package and import declarations,
• comments,
• variables,
• methods,
• constructors,
• initialization blocks,
• nested classes,
• nested interfaces, these data types are not
• annotations, and on the 1Z0-808 exam
• enums.
The structure of the source code file directly affects the compilability need to recognize misplaced stats:
Java classes are kept inside packages, which group together related classes and interfaces. The
packages also provide access protection and namespace management.
The import stat is used to import public classes and interfaces from other packages.
EXTRA: If a needed import statement is missing, classes and interfaces should be referred to by
their fully qualified names (in the form of pack.[subpack.]type_name).
Classes can be imported by class name or wildcard. Wildcards do not make the compiler look inside
subpackages (which are mapped to the local file system as subfolders).
EXTRA: In the event of a conflict, class name imports take precedence over wildcards.
package and import statements are optional. If present, they apply to all the classes and interfaces
defined in the same .java file.
Fields and methods are also optional but, unlike the package and import stats that must precede the
class declaration, they may be placed in any order within the class;
If a class defines a package statement, it should be the first statement in the .java file. It is a comperr
if the package stat appears inside or after the class declaration;
If a class has a package statement, all the imports should follow it.
A class can contain exactly one package statement + multiple import statements;
The import statement uses simple names of classes and interfaces from within the class.
The import statement cannot be applied to multiple classes or interfaces with the same simple
name;
Redundant imports are allowed.
EXTRA: The import statement requires the dot operator. 11
JAVA CLASS STRUCTURE, cont’d
Comments are another component of a class: they are used to annotate Java code and can appear
anywhere within the source code file.
EXTRA: Comments can contain any characters from the entire Unicode charset.
A comment can appear in multiple places, before or after a package statement, before, within, or after the
class definition, and before, within, or after the bodies of methods, constructors (ATTN), blocks, loops and
so on.
A Java class may define zero or more members such as static fields, instance variables, methods, or
constructors whose definitions can be placed in any order within the class.
Corollary: Since the declaration order does not matter, a method may use, for example, an instance
variable even before it has been declared in the file.
12
JAVA CLASS STRUCTURE, cont’d
13
1.3
main() method, etc.
14
main() method, cont’d
Before any Java class can be used by the JVM, it must be compiled into bytecode:
15
main() method, cont’d
To make the class executable, its main() method must be both public and static.
A class can define multiple methods with the name main(), provided that their signatures
do not match the signature of the main() method defined as the program’s entry point.
The ‛entry-point’ main() accepts an array of type String that will contain the parameters
specified on the command line after the class name. In fact, the main() method uses
copies of these parameters as its arguments.
Arguments are referenced starting with args[0]. An attempt to access an argument that
wasn’t passed in causes an RTE, namely, AIOOBE.
EXTRA: String[] args in public static void main() is never null. If the program is run without
any command line arguments, args points to a String array of zero length.
16
1.4
IMPORTING
PACKAGES
17
IMPORTING PACKAGES
Java code is commonly organized into packages, which resemble file system folders.
To reference public classes or interfaces contained in other packages, we can use:
• either an import statement, or
• the fully qualified name of the class/interface.
No import statement can be placed before a package declaration.
An import statement can end with either simple name of the class or a wildcard symbol, *.
If an import statement ends with the asterisk, it means that the code wants to have access
to all public data types in that particular package.
On the other hand, the wildcard symbol does not provide access to any of the subpackages
that may exist inside that one.
The java.lang package is the only package the compiler imports automatically.
Packages can contain multiple subpackages depending on how programmers organize their
classes and interfaces.
18
IMPORTING PACKAGES, cont’d
By default, data types in different packages and subpackages aren’t visible to each other.
On the other hand, all classes and interfaces within the same package are visible to each other.
The package and subpackage names are chained together with the dot operator.
An import statement is nothing but a handy way to tell the compiler where to look for a specific
public class or interface;
Corollary: Instead of using an import stat, we can always write the fully qualified name of the
public class or interface right in our code – but it soon becomes tedious.
EXTRA: It also follows that import statements can’t be used to access multiple classes or
interfaces that share the same simple name but reside in different packages. This problem can
be solved, for example, by using an import stat for one conflicting class and the fully qualified
name for its competitor. Another approach is to re-think the entire architecture of your
namespace.
19
IMPORTING PACKAGES, cont’d
An import stat allows to gain access to either a single public member of the package,
or to all of them (by using the above-mentioned wildcard symbol).
Known trap on the exam: If a data type isn’t public, it cannot be imported:
Unlike the asterisk used to access file system subfolders, import stat’s wildcard symbol
does not work in a similar fashion with subpackages. In other words, * does not import
subpackages.
20
IMPORTING PACKAGES, cont’d
EXTRA: If the source code file does not contain a package declaration, the class / interface
is considered to be a member of the so-called default package.
EXTRA: The default package’s members are accessible only to the classes or interfaces
defined in the same directory that contains the default package.
EXTRA: A class belonging to a default package can’t be used in any named packaged class
regardless of whether it is defined within the same directory, or not.
It is possible to import an individual public static member of a class or all of its public
static members by using an import static declaration.
To import a static method, we must use its name only, that is, without parentheses.
If the code appears clean and the LOCs are numbered, be on guard for missing imports for:
lists (List and ArrayList)
predicative lambdas
LDT-related LOCs
Arrays (e.g., Arrays.asList())
Collections (e.g., Collections.sort()) 21
1.5
JAVA OOP
FEATURES
22
JAVA OOP FEATURES
23
JAVA OOP FEATURES, cont’d
Java does not support multiple inheritance. Each class is permitted to extend only one class,
but can implement multiple interfaces.
EXTRA: Abstraction allows to develop data types in terms of their own functionality instead of
implementation details. Java provides support for abstract classes that expose interfaces to
the outside world without including the actual implementation of all methods. Basically,
abstraction aims to separate the implementation details of a class from its behavior.
EXTRA: Difference between abstraction and encapsulation:
Abstraction and encapsulation are complementary concepts: while abstraction deals
with the behavior of an object, encapsulation concerns the implementation details of this
behavior.
Encapsulation is usually achieved by hiding information about the internal state of an
object and thus can be seen as a way to realize abstraction.
Java is platform independent, meaning that it allows to develop applications that can run,
without having to be rewritten or recompiled, on any platform that has Java Runtime
Environment.
25
WARMING-UP
EXAM OBJECTIVE 1,
WARMING-UP EXERCIS
ES
26
TIME TO EXERCISE
TEST
27