Basic Java Syntax: Keywords (static, final, this, super)
The Java language is rich in keywords that define how language components are used. In this context, we will address four essential keywords in Java programming: static
, final
, this
and super
. Each of these keywords has a specific role in defining the structure and behavior of classes and objects.
Keyword static
The static
keyword is used to indicate that a member of a class (be it a field, a method or a block of code) belongs to the class itself, rather than to an instance specific to this class. This means that the static member can be accessed without needing to create an instance of the class.
public class ExampleStatic {
public static int counter = 0;
public ExampleStatic() {
counter++;
}
public static void static method() {
System.out.println("This is a static method.");
}
}
In the example above, counter
is a static field and StaticMethod
is a static method. Both can be accessed directly through the class name, as in ExemploStatic.contador
and ExemploStatic.metodoEstatico()
.
Keyword final
The final
keyword is used to declare a constant in Java. When a field is declared final, its value cannot be modified after initialization. Additionally, final
can also be applied to methods and classes. A final method cannot be overridden by subclasses, and a final class cannot be extended.
public class FinalExample {
public final int CONSTANT_VALUE = 10;
public final void final method() {
System.out.println("This method cannot be overridden.");
}
}
In the example above, CONSTANT_VALUE
is a constant and finalmethod
is a method that cannot be overridden.
Keyword this
The this
keyword is a reference to the current object, that is, the object whose method or constructor is being called. It is often used to resolve ambiguities between class fields and method parameters or constructors that have the same name.
public class ExampleThis {
private int value;
public ExampleThis(int value) {
this.value = value; // The 'this' keyword resolves the ambiguity.
}
public void setValue(int valor) {
this.value = value; // Again, 'this' is used to reference the class field.
}
public int getValor() {
return this.value; // 'this' is optional here, but can be used for clarity.
}
}
In the example above, this.value
refers to the value
field of the object, while value
without this
refers to the parameter of the constructor or method setValor
.
Keyword super
The super
keyword is used to access members (fields, methods) of the superclass of a class. This is particularly useful in cases of inheritance, where a derived class needs to reference members of the base class that have been overridden or hidden.
public class Base {
public void displayMessage() {
System.out.println("Base class message");
}
}
public class Derived extends Base {
public void displayMessage() {
super.displayMessage(); // Call the superclass method.
System.out.println("Derived class message");
}
}
In the example above, the Derived
class overrides the displayMessage
method. Within this method, super.exibirMensagem()
is used to invoke the implementation of the method in the Base
superclass, before executing additional code from the Derived
.
In summary, the keywords static
, final
, this
, and super
are fundamental to programming in Java. They allow developers to create clearer, safer and better-structured code, making programs easier to maintain and understand. By mastering the use of these keywords, programmers can further explore the object-oriented capabilities that Java offers.