OCA Java SE 8 Mock Exam Review - Java Source Code Structure Review








The structure of a Java class and source code file:

The following sections can be defined within a Java class:

  • import and
  • package statements
  • variables,
  • constructors,
  • methods,
  • comments,
  • nested classes,
  • nested interfaces,
  • annotations, and
  • enums.




Package

If a class has a package statement, it should be the first statement in the class definition. The package statement can't appear within a class declaration or after the class declaration.

If present, the package statement should appear exactly once in a class.

Java packages group together a related set of classes and interfaces.

By default, all classes and interfaces in separate packages and subpackages aren't visible to each other.

The package and subpackage names are separated using a dot . sign.

All classes and interfaces in the same package are visible to each other.





Import

A class can include multiple import statements.

If a class has a package statement, import statements should follow the package statement.

An import statement allows the use of simple names for packaged classes and interfaces from other packages.

You can't use the import statement to access multiple classes or interfaces with the same names from different packages.

You can import either a single member or all members of a package using the import statement.

You can't import classes from a subpackage by using the wildcard character in the import statement.

You can import an individual static member of a class or all its static members by using a static import statement.

An import statement can't be placed before a package statement in a class.

Comments

Comments are component of a class.

Comments are used to annotate Java code and can appear at multiple places within a class.

A comment can appear before or after a package statement, before or after the class definition, and before, within, or after a method definition.

Comments has two forms: multiline and end-of-line comments.

Comments can contain any special characters including Unicode charset.

Multiline comments span multiple lines of code. They start with /* and end with */.

End-of-line comments start with // and, as the name suggests, are placed at the end of a line of code. The text between // and the end of the line is treated as a comment.

Class

Class declarations and class definitions are components of a Java class.

A Java class may define zero or more instance variables, methods, and constructors.

The order of instance variables, constructors, and methods doesn't matter in a class.

A class may define an instance variable before or after the definition of a method.

Java source file

A Java source code file (.java file) can define multiple classes and interfaces.

A public class can be defined only in a source code file with the same name.

package and import statements apply to all the classes and interfaces defined in the same source code file (.java file).