OCA Java SE 8 Building Blocks - Java Class OCA Summary








Class

Java classes consist of members called fields and methods.

An object is an instance of a Java class.

Comments

There are three styles of comment: a single-line comment (//), a multiline comment (/* */), and a Javadoc comment (/** */).

main() method

Java begins program execution with a main() method.

The most common signature for this method run from the command line is

public static void main(String[] args).  

Arguments indexed starting with 0 are passed in to main method after the class name, as in java NameOfClass firstArgument.

Arguments are referenced starting with args[0]. Accessing an argument that wasn't passed in will cause the code to throw an exception.





package

Java code is organized into folders called packages. Packages contain Java classes.

To reference classes in other packages, you use an import statement.

Classes can be imported by class name or wildcard. A wildcard ending import statement to import all classes in that package. Wildcards do not look at subdirectories.

java.lang is a special package which is imported automatically.

In the event of a conflict, class name imports take precedence.

Constructor

Constructors create Java objects.

A constructor is a method with the same name as the class name and has no return type.

When an object is instantiated, fields and blocks of code are initialized first. Then the constructor is run.





Data types

Primitive types are the basic building blocks of Java types.

They are assembled into reference types.

Reference types can have methods and be assigned to null.

The numeric literals can begin with 0 (octal), 0x (hex), 0X (hex), 0b (binary), or 0B (binary).

Numeric literals can have underscores between two other numbers.

Variable

Declaring a variable has two parts: data type and variable a name.

Multiple variables can be declared and initialized in the same statement when they have the same type.

Variables representing fields in a class are automatically initialized to their corresponding "zero" or null value during object instantiation.

Local variables must be specifically initialized.

Identifiers can contain letters, numbers, $, or _. Identifiers cannot begin with numbers.

Variable scope

All variables go into their scope when declared.

Variable scope is the code block where a variable can be accessed.

There are three kinds of variables in Java, depending on their scope:

  • instance variables,
  • class variables, and
  • local variables.

Instance variables are the nonstatic fields of your class. Instance variables go out of scope when the object is garbage collected.

Class variables are the static fields within a class. Class variables remain in scope as long as the program is running.

Local variables are declared within a method. Local variables go out of scope when the block they are declared in ends.

Class File

Package and import statements are optional. If present, both go before the class declaration in that order.

The package statement comes first if present. Then comes the import statements if present. Then comes the class declaration.

Fields and methods are optional and are allowed in any order within the class declaration.

Garbage collection

Garbage collection can delete objects from memory when they can never be used again.

An object are eligible for garbage collection when it has no references to it or its references are all out of scope.

The finalize() method will run once for each object when it is first garbage collected.

Java object oriented

Java code is object oriented and all code is defined in classes.

Access modifiers allow classes to encapsulate data.

Java is platform independent, compiling to bytecode.

It is robust by not providing pointers or operator overloading.

Java is secure since it runs inside a virtual machine.