OCA
Java Basic
1
THE STRUCTURE OF A JAVA CLASS
AND SOURCE CODE FILE
THE STRUCTURE OF A JAVA CLASS
AND SOURCE CODE FILE
1.1.1 Structure of a Java class
1.1.2 Structure and components of a Java
source code file
1.1.1 STRUCTURE OF A JAVA CLASS
A
class can define multiple components
List of the components of a class:
The
package statement
The import statement
Comments
Class declarations and definitions
Variables
Methods
Constructors
1.1.1 STRUCTURE OF A JAVA CLASS
Java class components
Package statement
Import statements
Comments
Class declaration {
Variables
Comments
Constructors
Methods
Nested classes
Nested interfaces
Enum
}
Not included in OCA Java SE
7
5
1.1.1 STRUCTURE OF A JAVA CLASS
PACKAGE STATEMENT
All Java classes are part of a package
A Java class can be explicitly defined in a named
package
Otherwise it becomes part of a default package,
which doesnt have a name
A package statement is used to explicitly define
which package a class is in
If a class includes a package statement, it must
be the first statement in the class definition
6
1.1.1 STRUCTURE OF A JAVA CLASS
The package statement cannot appear within a class declaration or after
the class declaration.
The following code will fail to compile:
1.1.1 STRUCTURE OF A JAVA CLASS
The package statement must appear exactly once
in a class.
The following code wont compile:
1.1.1 STRUCTURE OF A JAVA CLASS
Classes and interfaces in the same package can
use each other
But to use a class or an interface from another
package
IMPORT STATEMENT
1.1.1 STRUCTURE OF A JAVA CLASS
IMPORT STATEMENT
10
1.1.1 STRUCTURE OF A JAVA CLASS
What happens if the class AnnualExam isnt
defined in a package?
11
1.1.1 STRUCTURE OF A JAVA CLASS
Note: the import statement follows the package
statement but precedes the class declaration
Reversing this order will result in your code
failing to compile
12
1.1.1 STRUCTURE OF A JAVA CLASS
COMMENTS
We can also add comments to your Java code
Comments can appear at multiple places in a
class.
A comment can appear
Before
and after a package statement
Before and after the class definition
before and within and after a method definition
Comments come in two flavors:
Multiline
comments
End-of-line comments
13
1.1.1 STRUCTURE OF A JAVA CLASS
Multiline comments: They start with /* and end with */
Multiline comments can contain any special characters
(including Unicode characters)
14
1.1.1 STRUCTURE OF A JAVA CLASS
We sometime use asterisk (*) to start the
comment in the next line.
This isnt required - its done more for aesthetic
reason
15
1.1.1 STRUCTURE OF A JAVA CLASS
End-of-line comments : start with //, no end sign
Placed at the end of a line of code.
The text between // and the end of the line is reated
as a scomment
16
1.1.1 STRUCTURE OF A JAVA CLASS
A comment can precede a package statement
The multiline comment is placed before the package
statement, which is acceptable because comments can appear
anywhere in your code.
17
1.1.1 STRUCTURE OF A JAVA CLASS
CLASS DECLARATION
The class declaration marks the start of a class
It can be as simple as the keyword class followed by the
name of a class:
18
1.1.1 STRUCTURE OF A JAVA CLASS
The declaration of a class is composed of the
following parts:
Access
modifiers (public, private, protected)
Nonaccess modifiers (final, static)
Class name
Name of the base class, if the class is extending
another class
All implemented interfaces, if the class is
implementing any interfaces
Class body (class fields, methods, constructors),
included within a pair of curly braces, {}
19
1.1.1 STRUCTURE OF A JAVA CLASS
Example
The following list summarizes the optional and
compulsory components.
20
1.1.1 STRUCTURE OF A JAVA CLASS
21
1.1.1 STRUCTURE OF A JAVA CLASS
CLASS DEFINITION
A
class is a design used to specify the properties and
behavior of an object
The properties of an object are implemented using
variables
and the behavior is implemented using methods.
A class is a design from which an object can be
created
22
1.1.1 STRUCTURE OF A JAVA CLASS
23
1.1.1 STRUCTURE OF A JAVA CLASS
Points to remember
A
class name starts with the keyword class. (not
Class)
The state of a class is defined using attributes or
instance variables
The behavior is defined using methods
A class definition may also include comments and
constructors
NOTE A class is a design from which an object
can be created.
24
1.1.1 STRUCTURE OF A JAVA CLASS
VARIABLES
Each object has its own copy of the instance
variables
All properties can be call instance variables or
instance attributes.
The instance variables are defined within a class but
outside all methods in a class.
Class variable or Static variable is shared by all the
objects of a class
25
1.1.1 STRUCTURE OF A JAVA CLASS
METHODS
All
method is call Instance methods
A class method or static method is used to work with
the static variables,
CONSTRUCTORS
Use
to create and initialize the objects of a class.
A class can define multiple constructors that accept
different sets of method parameters
26
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
A Java source code file is used to define classes
and interfaces
All your Java code should be defined in Java
source code files (file.java)
Structure of a Java source code file
Definition
of a class and an interface
Definition of single or multiple classes and interfaces
(in the same file)
Application of import and package statements to all
the classes in a Java source
code file
27
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
DEFINITION OF INTERFACES
An
interface is a grouping of related methods and
constants
But the methods in an interface cannot define any
implementation
An interface specifies a contract for the
classes to implement.
28
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
The definition of an interface starts with the
keyword interface.
An interface can define constants and methods.
29
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
DEFINITION OF SINGLE AND MULTIPLE
CLASSES IN A SINGLE JAVA SOURCE CODE
FILE
we
can define either a single class or an interface in a
single Java source code file
30
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
We can also define a combination of classes and
interfaces in the same Java source code file.
31
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
32
1.1.2 STRUCTURE AND COMPONENTS
OF A JAVA SOURCE CODE FILE
33
IMPORT STATEMENTS IN JAVA
The java source file can have multi class or interface
The import statements can apply in all class or
interface
Classes and interfaces defined in the same Java
source code file cant be defined in orther packages
34