Final static variables can be accessed anywhere

Java - programming paradigms, architecture principles, data structures & algorithms

back to the listNote:
  1. It is assumed that the inclined reader has mastered the Java language at ALP2 level. In this respect, the main difficulty in writing was to reduce it to the essentials, as you had to find the balance between comprehensibility and repetition of the familiar. So if in doubt, do some research (see also links) or skip.
  2. Since there are no functions in Java, only methods (which work on objects), the two terms are used synonymously here.

Object orientation

Attributes & visibility

An identifier (a variable) x called visible at a point in the program if it was agreed in one of the blocks surrounding the point in the program. A distinction is made between classes within global and local Variables. While global variables are valid within the whole class, local variables have only a limited scope (within a method, loop or block).

Local variables hide global ones if they have the same identifier. That means: Does a global variable exist? a and a method redefines a variable a, the local variable is always addressed within the method by the identifier 'a'. The global variable can be accessed explicitly using the this keyword.

The visibility of variables can be influenced by the public, private and protected attributes:

visibilityWithin the packageDerived classesOutside the package
privateinvisibleinvisibleinvisible
defaultvisibleinvisibleinvisible
protectedvisiblevisibleinvisible
publicvisiblevisiblevisible
Java classes can either be given the attribute public (then they can be reached from anywhere) or they can be attribute-free. In this case they have default-Visibility and can only be accessed by classes from the same package.

Example:

package vehicles; public class Auto {private int a = 1; / * a is global variable in automobile and through the attribute * private invisible outside of class. Assignments * of the type myAuto.a = 5; are not possible. The variable * is not inherited. * / public int b = 2; / * b is global variable in automobile and through the attribute * public accessible from anywhere. Instances of the class * accept assignments of the type myAuto.b = 8; * / protected int c = 3; / * c is global variable in automobile. Through the attribute * protected the visibility is limited to the same package *. The variable is inherited by derivatives of Auto *. * / public int getNumber (int x) {int a = 4; / * a is local variable in getNumber () and hides * within getNumber () the global variable a * / if (x < 10)="" return="" a;="" gibt="" 4="" zurück="" if="" (x=""> 20) return b; // Returns 2. (The global variable is // valid everywhere in Auto return this.a; // outputs 1. (The keyword // this addresses the global variable a //} private string getPolicy () {return ("This method can can only be used within Auto. "+" It is not inherited. ");} protected String getLiberalPolicy () {return (" This method can be used within Auto and from all "+" classes of the Package vehicles be used. "+" It is inherited by derived classes. ");}}

Further attributes in Java are static, final and the (rather special and therefore not described here) attributes transient and volatile.

static
The static keyword is applicable to variables and methods. If a variable or method is assigned the static attribute, it only exists once for all instances of the class together. Static variables are so-called class variables. If a class changes a static variable, it is also changed for all other instances.
Since static variables and methods do not belong to a specific instance, they can also be accessed directly via the class name without first having to create an instance of the class.

final
The final keyword is applicable to variables, methods, and classes.

  • Final variables must be initialized and can no longer be changed at runtime. (They replace the constants used in other languages.)
  • Final methods cannot be overridden by derived classes.
  • Final classes cannot be derived, they end the chain of inheritance and can no longer be extended.
Since elements that cannot be changed with the final attribute, the virtual machine can dispense with reloading variables or dynamic method searches during runtime, which is why final elements can often be processed with significantly higher performance.

Principle of secrecy

([principle of] information hiding) denotes a design principle for modules according to [Parnas '72]. Modules should hide their concrete implementation. ("Every module has a secret!")
Components of a software system are viewed as black boxes that only show relevant information to the outside world. In Java, the principle of secrecy is implemented using interfaces and the private and protected attributes.
The advantages lie in the consistent modularization: Interchangeability of the implementation of a class (while retaining the interface).
The secret principle is also important for → abstract data types.

Polymorphism

Polymorphism (Latin for diversity) describes the property of a variable to be able to stand for objects of different classes. There are several types of polymorphism:

  • Universal polymorphism
    Universal polymorphic functions manipulate objects with similar properties in the same way. A distinction is made between inclusion polymorphism and parametric polymorphism:
    • Inclusion polymorphism (inheritance) - An instance variable can also accept objects from subclasses. A variable of type motorcycle except motorcycle objects, including those of the type Suzuki or Harley Davidson take up.
    • Parametric polymorphism (type variables) - a method accepts parameters of general type (Genericity). In Java this is done by methods that take the type Object accept realized. (Which is actually inclusion polymorphism - let's see what comes with Java 1.5 ...) In Haskell there is the general parameter a.
  • Ad hoc polymorphism (overloaded)
    In the case of ad hoc polymorphism, the various parameters do not have a common structure. The different types are treated differently. One achieves this through Overloaded of methods: A method can exist several times for different parameters.

Assertions

Assertions are used to check invariants in order to make programs more robust. To do this, the program is instructed with the keyword assert (from Java 1.4) to check a condition for truth. If this does not apply (usually contrary to expectations), an exception is raised. The compiler can be instructed via parameters to enable or disable the assertions, so that they are only used during development, for example, and are omitted for the final version of a program for runtime optimization.

Parameter transfer: Call by value vs. Call by reference

Call by value

The parameter of a method corresponds to a local variable that is initialized with the transferred value when called (parameter copy). If a method changes the parameter, this only affects its local copy, not the transferred variable. In Java all primitive data types are transferred by call by value.

Call by reference

Parameters are transferred as a reference to a variable. If a method changes the parameter, the transferred variable is also changed. In Java objects are passed as pointers so that changes always apply globally.

Call by result

(Partly also call-by-value-return, also at Schweppe.) Here, the result is first calculated and then returned in parameter variables.

Example (no java!):

void solve (in float p, in float q, out float x1, out float x2) {float root = sqrt (p * p / 4 - q); x1 = -p / 2 + root; x2 = -p / 2 - root; }

Call by name

Corresponds to lazy evaluation such as in Haskell. From the result like Call by reference, but with different semantics: The passed Identifier replaces the parameter of the method declaration when called, so similar to macros in C. A kind of automatic copy'n'paste. The ALGOL language uses this system, which is rarely used in modern languages. (see also FOLDOC: call-by-name)

Design pattern

Design pattern Design patterns) are - in software engineering - solutions for common problem types in program design. The use of design patterns should make the code more robust, elegant and faster, since the wheel is not always reinvented, but instead tried and tested solutions are used. (So ​​the problem lies more in finding the right solution from the known ones.)

Commonly used design patterns

  • Singleton - A class of which only one instance can be created
  • Immutable - A class that cannot be changed after initialization
  • Interface - Separates properties from implementation
  • Iterator - Iterates through all elements of a collection
  • Factory - Takes over the initialization and instantiation of different classes with common properties

Data structures & implementations

Left

Java is an island too
Comments on the (current) deficits, e.g. generics