-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnnamedClassesAndInstanceMain.java
More file actions
72 lines (60 loc) · 2.49 KB
/
Copy pathUnnamedClassesAndInstanceMain.java
File metadata and controls
72 lines (60 loc) · 2.49 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
62
63
64
65
66
67
68
69
70
71
package java21.unnamedclasses;
/**
* Java 21 Unnamed Classes and Instance Main Methods (Preview) Demonstrates simplified class structure for simple programs
* NOTE: Requires --enable-preview flag for compilation and execution Compile: javac --enable-preview --release 21 UnnamedClassesAndInstanceMain.java Run: java --enable-preview UnnamedClassesAndInstanceMain
*/
public class UnnamedClassesAndInstanceMain
{
public static void main(String[] args)
{
System.out.println("=== Java 21 Unnamed Classes and Instance Main Methods (Preview) ===\n");
System.out.println("Example 1: Unnamed Class");
System.out.println("------------------------");
System.out.println("""
// File: Hello.java (no class declaration needed)
void main() {
System.out.println("Hello, World!");
}
// Compile: javac --enable-preview --release 21 Hello.java
// Run: java --enable-preview Hello
""");
System.out.println("\nExample 2: Instance Main Method");
System.out.println("-------------------------------");
System.out.println("""
// Instance main method (non-static)
void main() {
System.out.println("Hello from instance main!");
// Can access instance fields and methods
}
// Static main still works
public static void main(String[] args) {
System.out.println("Hello from static main!");
}
""");
System.out.println("\nExample 3: With Fields and Methods");
System.out.println("----------------------------------");
System.out.println("""
String greeting = "Hello";
void main() {
System.out.println(greeting + ", World!");
sayHello();
}
void sayHello() {
System.out.println("This is a method in unnamed class");
}
""");
System.out.println("\nKey Features:");
System.out.println("- No explicit class declaration needed");
System.out.println("- Instance main methods (non-static)");
System.out.println("- Simplified syntax for simple programs");
System.out.println("- Useful for learning and scripting");
System.out.println("- Compiler generates class automatically");
System.out.println("\nBenefits:");
System.out.println("- Reduces boilerplate code");
System.out.println("- Easier for beginners");
System.out.println("- Good for simple scripts");
System.out.println("- More intuitive for small programs");
System.out.println("\nNote: This is a preview feature in Java 21.");
System.out.println("Requires --enable-preview flag for compilation and execution.");
}
}