Posts

Import statements

Image
  9. Import statements 9.1. Usage of import statements You have to access a Java class always via its full-qualified name, i.e., the package name plus a . followed by the class name. You can add  import  statements into your class file. These allow you to use the related classes in your code without the package qualifier. 9.2. Static imports Static import allows  public static members (fields and methods) of another class to be used in Java code without a class reference. The feature provides a typesafe mechanism to include constants into code. It also improves code readability and allows Java API designers to write a concise API.

Modifiers

Image
  8. Modifiers 8.1. Access modifiers There are three access modifiers keywords available in Java: *  public  *  protected  *  private There are four access levels:  public , protected ,  default  and  private . They define how the corresponding element is visible to other components. If something is declared public, e.g., classes or methods can be freely created or called by other Java objects. If something is declared private, e.g., a method, it can only be accessed within the class in which it is declared. The access levels  protected  and  default are similar. A  protected  class can be accessed from the package and sub-classes outside the package, while a default  class can get accessed only via the same package. The following table describes the visibility: Table 1. Access Level Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N 8.2. Other modifier...

Variables and methods

Image
  7. Variables and methods 7.1. Variable Variables  allow the Java program to store values during the runtime of the program. A variable can either be a: *  primitive variable  *  reference variable A primitive variable contains the value. A reference variable contains a reference (pointer) to the object. Hence, if you compare two reference variables, you compare if both point to the same object. To identify if objects contain the same data, use the  object1.equals(object2)  method call. 7.2. Instance variable Instance variable is associated with an instance of the class (also called object). Access works over these objects. Instance variables can have any access control and can be marked  final  or  transient . Instance variables marked as final cannot be changed after a value has been assigned to them. 7.3. Local variable Local (stack) variable declarations cannot have access modifiers. Local variables do not get default values, so the...

Annotations in Java

Image
  6. Annotations in Java 6.1. Annotations in Java Annotations provide data about a class that is not part of the programming logic itself. They have no direct effect on the code they annotate. Other components can use this information. Annotations can be preserved at runtime ( RetentionPolicy.RUNTIME ) or are only available at development time ( RetentionPolicy.SOURCE ). 6.2. Override methods and the @Override annotation If a class extends another class, it inherits the methods from its superclass. If it wants to change these methods, it can  override  these methods, i.e., redeclare the methods. This is necessary for an abstract method unless the class itself is defined as abstract. The  @Override  annotation can be added to such a method. It is used by the Java compiler to check if the annotated method really overrides a method of an interface or the extended class. To override a method, you use the same method signature in the source code of the subclass. To i...

Java interfaces

Image
  5. Java interfaces 5.1. What is an interface in Java? An  interfaces  is a type similar to a class and is defined via the  interface keyword. Interfaces are used to define common behavior of implementing classes. If two classes implement the same interface, other code which work on the interface level, can use objects of both classes. Like a class an interface defines methods. Classes can implement one or several interfaces. A class which implements an interface must provide an implementation for all abstract methods defined in the interface. 5.2. Abstract, default and static methods in Interfaces An interface can have abstract methods and _default_methods. A default method is defined via the  default keyword at the beginning of the method signature. All other methods defined in an interfaces are public and abstract; explicit declaration of these modifiers is optional. Interfaces can have constants which are always implicitly public, static and final. The foll...