-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementsBeforeSuper.java
More file actions
62 lines (51 loc) · 1.59 KB
/
Copy pathStatementsBeforeSuper.java
File metadata and controls
62 lines (51 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package java22.constructors;
/**
* Java 22 Statements Before super() (Preview) Demonstrates statements allowed before super() call
* NOTE: This is a preview feature that requires special compilation flags. The linter errors you see are expected when preview is not enabled.
* To compile and run: javac --enable-preview --release 22 StatementsBeforeSuper.java java --enable-preview StatementsBeforeSuper
* The code structure is correct for Java 22 preview feature. Instance methods cannot be called before super() - only static methods are allowed.
*/
public class StatementsBeforeSuper
{
public static void main(String[] args)
{
Child child = new Child(" John ");
System.out.println("Child created: " + child.getName());
}
}
class Parent
{
private String name;
Parent(String name)
{
this.name = name;
System.out.println("Parent: " + name);
}
String getName()
{
return name;
}
}
class Child extends Parent
{
private String processedName;
Child(String name)
{
// Statements before super() - preview feature in Java 22
// Note: Requires --enable-preview flag to compile
if (name == null || name.isBlank())
{
throw new IllegalArgumentException("Name cannot be null or blank");
}
String processed = name.trim().toUpperCase();
String validated = validate(processed); // Static method call allowed
super(validated); // Now allowed after statements (preview)
// After super()
this.processedName = validated;
System.out.println("Child initialized with: " + processedName);
}
private static String validate(String s)
{
return s.length() > 0 ? s : "Default";
}
}