NESTED CLASSES
RECALL CLASS MEMBERS
Class members in object-oriented programming are essential components that describe the properties and behaviors of an object. They mainly include instance variables and methods which define the state and actions of an object, respectively.
When multiple objects are created from a class, each object has its own unique set of instance variables. This means that while they belong to the same class, the values of the instance variables are stored in separate memory locations, allowing for distinct behavior and characteristics.
Example: Bicycle class
Instance Variables: A Bicycle class typically includes instance variables like cadence, gear, speed, and id, which hold specific attributes relevant to each Bicycle object.
Each Bicycle object maintains its own values for these instance variables, which are isolated from other Bicycle objects, ensuring that changes to one object's state do not affect others.
Code Example:
public class Bicycle {
private int cadence;
private int gear;
private int speed;
private int id;
}
To ensure that each Bicycle can be identified uniquely (e.g., serial numbers), an instance variable for ID can be created, promoting better management of objects.
RECALL CLASS MEMBERS - STATIC VARIABLES
In certain situations, there are needs for shared variables that maintain the same value across all instances of a class. This is where static variables come into play. Unlike instance variables, static variables are allocated in a single memory location and are shared among all instances of the class. This feature is useful for tracking data that is relevant to the class as a whole rather than individual instances.
Example: If we want to track how many Bicycle objects have been instantiated, a static variable can be implemented.
Code Example:
private static int numberOfBicycles = 0;
RECALL CLASS METHODS - STATIC METHODS
Static methods are significant as they allow you to access static variables and methods without the necessity of creating an instance of the class. This facilitates interactions with class-level data more efficiently.
Syntax: The call to a static method typically follows the structure of ClassName.methodName(args).
Example: To access the static variable numberOfBicycles, you can define a static method as follows:
public static int getNumberOfBicycles() {
return numberOfBicycles;
}
CLASS MEMBERS - ACCESS RESTRICTIONS
The access restrictions for class members are crucial in influencing how methods and variables can be interacted with from different contexts.
Instance Methods can:
Access instance variables and instance methods directly, giving them the ability to manipulate the state of the object.
Access class variables and methods directly, allowing for simplified interactions with static members.
Class Methods (static methods) can:
Access static variables and methods directly.
Not access instance variables or methods directly; to do this, they require an object reference to access those members.
They also cannot utilize the
thiskeyword, as there is no instance to refer to.
NESTED CLASSES
A nested class is defined within the scope of another class and is useful when the nested class is solely relevant to its enclosing class. This arrangement leads to improved encapsulation and effective information hiding, facilitating better code organization.
Examples:
A Node class defined within a List class, where Nodes are only useful in the context of a List.
A Client class encapsulated within an Account class, emphasizing that a Client is directly tied to their respective accounts.
When compiled, this structure produces two .class files:
OuterClass.class
OuterClass$InnerClass.class
Code Example:
class OuterClass {
class InnerClass {
// Inner class content
}
}
TYPES OF NESTED CLASSES
There are two principal types of nested classes:
Static Nested Classes: These classes are declared within another class without needing an instance of the outer class. Static nested classes can only access the static members of the enclosing class and are beneficial when you don't need to reference instance variables.
Example Code:
class OuterClass {
static class StaticNestedClass {
void display() {
System.out.println("Hello from static nested class");
}
}
}
Inner Classes: Unlike static nested classes, inner classes require an instance of the outer class to be instantiated. Inner classes can access all members (including private) of the outer class, providing more flexibility in operations.
To instantiate an inner class:
Create an instance of the outer class.
Then create an instance of the inner class using the outer class instance.
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
INNER CLASSES
Inner classes can directly access private members of the outer class, which can be advantageous for modifying state or behavior without exposing those members outside the class structure. Inner class methods commonly access private members through outer class methods, promoting encapsulation and reducing visibility.
NESTED CLASSES ACCESSES
Nested classes possess unique access privileges:
They can access all members (both public and private) of the enclosing class.
The enclosing class can also access all private members of the nested class, which highlights the mutual accessibility between nested and enclosing classes.
Static nested classes, however, can only refer to static members of the outer class, limiting their context.
SIMPLE USES OF INNER CLASSES
Inner classes serve mainly to optimize organization and encapsulation within a class structure. They simplify access to the enclosing object's private methods and aid in logically grouping classes that are only locally relevant, enhancing code readability and maintainability.
ANONYMOUS CLASSES
Anonymous classes simplify class definition by allowing the creation and instantiation of a class without needing to name it explicitly. This feature is handy for situations where a class is needed for only a single-use case, such as event listeners or callbacks.
Example Code:
SomeInterface myObj = new SomeInterface() {
@Override
public void myMethod() {
// method body
}
};
LIMITATIONS OF LOCAL CLASSES
There are specific limitations when utilizing local classes:
Local classes cannot declare static members except for constants.
Local variables utilized within the definition of local classes must be final or effectively final to ensure consistent behavior.
Local classes can still access the members of the enclosing class, maintaining some flexibility in scope.
NESTED CLASS EXAMPLES
Example of an Inner Class accessing outer class members:
public class Outer {
private int outerVar;
class Inner {
void display() {
System.out.println(outerVar);
}
}
}
Example of a Static Nested Class:
class Outer {
static class Nested {
static int nestedVar = 5;
}
}
SUMMARY OF USAGE
In regards to class organization:
Inner Classes: They are best utilized for logically grouping classes that are not needed outside of their context, promoting encapsulation and preventing clutter in the global namespace.
Static Nested Classes: These are more suitable when the nested class requires its own structure, independent of the outer class’s instance variables, improving clarity in design.
Anonymous Classes: They are advantageous for situations that call for quick implementations specifically when naming a class is unnecessary, aiding in swift application development without extensive boilerplate code.