Junior — Middle
Can you name built-in classes in programming languages?
sobes.tech AI
Answer from AI
In Java, nested (inner) classes are classes declared within another class. They come in several types:
-
Static nested classes: declared with the
staticmodifier. They do not have access to the non-static members of the outer class. -
Inner classes: non-static classes within another class. They have access to the members of the outer class.
-
Local classes: declared within a method, accessible only within that method.
-
Anonymous classes: classes without a name, declared and instantiated simultaneously, often used for implementing interfaces or inheritance.
Example of an inner class:
public class Outer {
private int x = 10;
class Inner {
void print() {
System.out.println(x); // access to outer class field
}
}
}
Nested classes help logically group code and provide encapsulation.