In Apex, the static
keyword associates variables and methods with the class itself, not instances, meaning they’re initialized only once and can be accessed directly by class name. The final
keyword defines constants or methods that cannot be overridden in subclasses, maintaining consistent behavior and values across your codebase.
The this
keyword in Apex represents the current instance of a class and is often used in constructor chaining to clarify variable scope. super
allows access to a superclass’s constructor or methods, enabling subclasses to inherit or extend base class functionality. These keywords together help manage memory, maintain integrity, and control inheritance.
Important keywords in Apex Programming
Below are some of the important keywords in Apex programming language.
Here’s an explanation of the provided examples for each keyword:
1. static
The static
keyword in Java and Apex associates a variable or method with the class itself rather than any instance of the class. This means it’s initialized only once and can be accessed directly via the class name without creating an object instance.
java public class OuterClass { public static final Integer MY_INT; static { MY_INT = 10; } }
Here, MY_INT
is a static variable initialized in a static block, meaning it’s set only once when the class is loaded and can be accessed as OuterClass.MY_INT
without instantiating OuterClass
.
2. final
final
is used to define constants (variables that don’t change) or methods that cannot be overridden in subclasses. Once assigned, a final
variable cannot be modified.
java public class myCls { static final Integer INT_CONST; }
INT_CONST
is defined as a final
static variable, meaning it’s a constant that can’t be altered after its initial assignment.
3. this
this
refers to the current instance of a class and is commonly used for differentiating instance variables from parameters or for constructor chaining.
java public class Foo { public Foo(String s) { /* ... */ } public Foo() { this("memes repeat"); } }
Here, this("memes repeat");
calls another constructor within the same class, passing "memes repeat"
as a parameter.
4. super
super
is used to call a constructor or method from a superclass, allowing access to inherited behavior.
java public class AnotherChildClass extends InnerClass { AnotherChildClass(String s) { super(); // Calls the no-argument constructor of InnerClass } }
super()
calls the no-argument constructor of InnerClass
, allowing AnotherChildClass
to inherit its behavior or initialize inherited properties.
5. return
The return
keyword exits a method and optionally returns a value.
java public Integer sum() { return int_var; }
In this example, return int_var;
exits the sum
method and provides int_var
as the result.
6. transient
In Apex, transient
is used to declare variables that should not be saved as part of a Visualforce view state, meaning they won’t persist across different pages or sessions.
java transient Integer currentValue;
currentValue
will not be stored in the view state, which optimizes memory and performance in Visualforce pages.
7. null
null
represents an absence of value. It can be assigned to any variable to indicate that it doesn’t point to any data or object.
java Boolean b = null;
The Boolean
variable b
is explicitly set to null
, meaning it has no value or state until it is initialized.
These explanations clarify the purpose and use of each keyword in Apex and Java. These keywords are fundamental to managing memory, inheritance, and object behavior in object-oriented programming.