OCA Java SE 8 Mock Exam Review - Java class definition Review








Executable Java applications

An executable Java class is a class with the main method.

The main method has the signature

public  static  void  main(String  args[])  or  
public  static  void main(String...  args).  

The positions of static and public can be interchanged, and the method parameter can use any valid name.

A class can define multiple methods with the name main with proper overloading signature. These other methods with different signatures aren't considered the main method.

The main method accepts an array of type String containing the method parameters passed to it by the JVM.

The keyword java and the name of the class aren't passed on as command parameters to the main method.





Java access modifiers

The access modifiers control the accessibility of your class and its members outside the class and package.

Java defines four access modifiers: public, protected, default, and private.

The public access modifier is the least restrictive access modifier.

Classes and interfaces defined using the public access modifier are accessible to related and unrelated classes outside the package in which they're defined.

The members defined with protected modifier are accessible to classes and interfaces defined in the same package and to all derived classes.

The members defined without using an explicit access modifier are defined with package accessibility (also called default accessibility).

The members with package access are accessible only to classes and interfaces defined in the same package.

A class defined using default access can't be accessed outside its package.

The members defined with private access modifier are accessible only to the class in which they are defined.

Private members are not accessible outside the class in which they're defined.

The private access modifier is the most restrictive access modifier.





Abstract class interface

The abstract keyword prefixed to a class change the class to an abstract class, even if it doesn't define any abstract methods.

An abstract class cannot be instantiated.

An interface is an abstract entity by default.

The Java compiler automatically adds the keyword abstract to the definition of an interface.

Adding the keyword abstract to the definition of an interface is redundant.

Abstract method

An abstract method doesn't have a body, which means it's implemented by the class that extends the class defining the abstract method.

A variable can't be defined as an abstract variable.

A method can't be defined both as abstract and static.

Static

static fields and methods are common to all instances of a class and aren't unique to any instance of a class.

static attributes exist independent of any instances of a class and may be accessed before instances of the class are created.

static attributes are known as class fields or class methods because they're said to belong to their class, not to any instance.

A static variable or method can be accessed using the name of a reference object variable or the name of a class.

A static method or variable can't access non-static variables or methods of a class.

Non-static variables and methods can access static variables and methods.

You can't prefix the definition of a top-level class or an interface with the keyword static.

A top-level class or interface is one that isn't defined within another class or interface.