Generate comprehensive documentation for Java record classes with examples and best practices
Generate complete documentation for Java record classes, including syntax, examples, validation patterns, and best practices. This skill helps developers understand and implement Java records effectively for immutable data carriers.
This skill creates comprehensive documentation for Java record classes based on the official Java SE documentation. It covers:
When a user requests Java record documentation:
1. **Analyze the request** to determine the specific aspect of Java records they need:
- Basic record syntax and structure
- Constructor patterns (canonical vs compact)
- Validation and constraints
- Advanced features (generics, sealed classes, annotations)
- Nested or local records
- Static members and methods
- Integration patterns
2. **Generate structured documentation** including:
- Clear explanation of the concept
- Syntax examples with proper formatting
- Equivalent normal class code (where helpful for understanding)
- Practical use cases
- Common pitfalls and restrictions
3. **Include code examples** that demonstrate:
- Basic record declaration with components
- Validation using compact constructors
- Custom accessor methods when needed
- Static fields, initializers, and methods
- Generic record classes
- Records implementing interfaces
- Annotated record components
- Local record classes in method bodies
- Nested record classes
4. **Highlight key restrictions**:
- Records are implicitly final (cannot be extended)
- Cannot declare instance variables or instance initializers
- Cannot declare native methods
- Component fields are automatically private and final
- Inner classes can contain record members (Java SE 16+)
5. **Explain automatic member generation**:
- Private final fields for each component
- Public accessor methods matching component names
- Canonical constructor
- equals(), hashCode(), and toString() implementations
6. **Cover validation patterns**:
- Using compact constructors for validation
- Throwing IllegalArgumentException for invalid data
- Difference between canonical and compact constructor syntax
7. **Show advanced patterns**:
- Generic record classes with type parameters
- Records as permitted subclasses in sealed hierarchies
- Annotation propagation from components to fields
- Static factory methods (e.g., createGoldenRectangle pattern)
- Records in stream operations
8. **Format the output** as:
- Markdown with proper code blocks using ```java
- Clear section headings
- Inline comments explaining key points
- Comparison examples (record vs normal class) when clarifying
**User:** "Show me how to create a Java record with validation"
**Output:**
```markdown
```java
record Rectangle(double length, double width) {
// Compact constructor with validation
public Rectangle {
if (length <= 0 || width <= 0) {
throw new IllegalArgumentException(
String.format("Invalid dimensions: %f, %f", length, width));
}
// No explicit field assignment needed - automatic at end
}
}
```
```java
record Rectangle(double length, double width) {
// Explicit canonical constructor
public Rectangle(double length, double width) {
if (length <= 0 || width <= 0) {
throw new IllegalArgumentException(
String.format("Invalid dimensions: %f, %f", length, width));
}
this.length = length; // Required in canonical form
this.width = width; // Required in canonical form
}
}
```
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/java-records-documentation-generator/raw