-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnnamedClassesAndInstanceMain.java
More file actions
88 lines (73 loc) · 2.9 KB
/
Copy pathUnnamedClassesAndInstanceMain.java
File metadata and controls
88 lines (73 loc) · 2.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package java24.unnamedclasses;
/**
* Java 24 Implicitly Declared Classes and Instance Main Methods (Finalized) Demonstrates simplified class structure for simple programs
* NOTE: This feature is now finalized - no preview flag needed!
*/
public class UnnamedClassesAndInstanceMain
{
public static void main(String[] args)
{
System.out.println("=== Java 24 Implicitly Declared Classes and Instance Main Methods (Finalized) ===\n");
System.out.println("Overview:");
System.out.println("--------");
System.out.println("Unnamed classes allow you to write simple programs without explicit class declarations.");
System.out.println("This feature is now finalized in Java 24.\n");
System.out.println("Example 1: Simple Unnamed Class");
System.out.println("------------------------------");
System.out.println("""
// File: Hello.java (no class declaration needed)
void main() {
System.out.println("Hello, World!");
}
// Compile: javac Hello.java
// Run: java Hello
// No --enable-preview flag needed!
""");
System.out.println("\nExample 2: Unnamed Class with Fields and Methods");
System.out.println("------------------------------------------------");
System.out.println("""
String greeting = "Hello";
int count = 5;
void main() {
System.out.println(greeting + ", World!");
sayHello();
printCount();
}
void sayHello() {
System.out.println("This is a method in unnamed class");
}
void printCount() {
System.out.println("Count: " + count);
}
""");
System.out.println("\nExample 3: Instance Main Method");
System.out.println("--------------------------------");
System.out.println("""
// Instance main method (non-static)
String name = "Java";
void main() {
System.out.println("Hello from " + name);
// 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("\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("- Finalized feature (no preview flag needed)");
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("- Production-ready (finalized)");
System.out.println("\nNote: This feature is finalized in Java 24.");
System.out.println("No --enable-preview flag needed!");
}
}