Skip to content

Commit e72fe67

Browse files
committed
Writing first Java class using IntelliJ
1 parent d6f8369 commit e72fe67

1 file changed

Lines changed: 175 additions & 5 deletions

File tree

section3/HelloWorld/README.md

Lines changed: 175 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Getting Started with Java in IntelliJ IDEA
2-
Welcome to our Java course! In this guide, we'll walk you through creating your first Java class using IntelliJ IDEA, a powerful integrated development environment (IDE) for Java development.
2+
Welcome to our Java course! In this guide, we'll walk you through creating your first Java class using IntelliJ IDEA, a powerful
3+
integrated development environment (IDE) for Java development.
34
### Prerequisites
45
Before getting started, ensure that you have the following installed on your system:
5-
- IntelliJ IDEA (Community or Ultimate edition) https://www.jetbrains.com/idea/download/?section=windows
6-
- Java Development Kit (JDK) installed on your machine https://docs.aws.amazon.com/corretto/latest/corretto-21-ug/downloads-list.html
6+
- IntelliJ IDEA (Community or Ultimate edition) https://www.jetbrains.com/idea/download/
7+
- Java Development Kit (JDK) installed on your machine https://docs.aws.amazon.com/corretto/latest/corretto-25-ug/downloads-list.html
78
### Steps to Create Your First Java Class
89
1. #### Open IntelliJ IDEA : Launch IntelliJ IDEA on your system.
910
2. #### Create a New Project :
@@ -13,7 +14,7 @@ Before getting started, ensure that you have the following installed on your sys
1314
- Choose a Project Location where you want to save your project files.
1415
- Select the Language as Java.
1516
- Build system as IntelliJ.
16-
- Add JDK as corretto-21 or click on New... and locate your JDK installation directory.
17+
- Add JDK as corretto-25 or click on New... and locate your JDK installation directory.
1718
- Click on Create to create the project.
1819
4. #### Create a Java Class :
1920
- In the Project tool window (usually located on the left-hand side), right-click on the src folder.
@@ -38,5 +39,174 @@ public class HelloWorld {
3839
- Click on the green Run icon next to the main method or right-click anywhere inside the main method and select Run 'HelloWorld.main()'.
3940
7. #### View Output :
4041
- You should see the output "Hello Madan" printed in the Run tool window at the bottom of the IntelliJ IDEA window.
41-
42+
43+
## Launch Single-File & Multi-File Source-Code Programs
44+
45+
The Single-File Source-Code Program feature, introduced in `Java 11`, allows developers to run Java programs directly from a source file without the need for separate compilation.
46+
47+
This was later enhanced in `Java 22` to support Multi-File Source-Code Programs, enabling the execution of Java applications that span across multiple `.java` files — still without explicit compilation.
48+
49+
### ⚙️ Traditional Java (Before Java 11)
50+
51+
Prior to `Java 11`, you had to compile your Java code before running it:
52+
53+
### 🧾 Example
54+
```java
55+
public class Hello {
56+
public static void main(String[] args) {
57+
System.out.println("Hello World...");
58+
}
59+
}
60+
```
61+
62+
▶️ Commands
63+
```
64+
javac Hello.java # Compilation
65+
java Hello # Execution
66+
```
67+
Two distinct steps were required — compile first, then run.
68+
69+
### 🚀 From Java 11 — Single-File Source Programs
70+
71+
Starting in Java 11, you can execute a Java program directly from its source file using a single command.
72+
73+
### 🧾 Example
74+
```java
75+
public class Hello {
76+
public static void main(String[] args) {
77+
System.out.println("Hello World...");
78+
}
79+
}
80+
```
81+
82+
▶️ Command
83+
```
84+
java Hello.java
85+
```
86+
✅ The JVM automatically compiles and runs the file in one step.
87+
88+
**⚠️ Limitation :** Works only if all the code is within a single file.
89+
If your logic spans multiple source files, Java 11 cannot handle it.
90+
91+
92+
### 🧱 From Java 22 — Multi-File Source Programs
93+
94+
Java 22 extends this functionality — now you can run Java programs that use multiple source files (with dependencies) without manual compilation.
95+
96+
### 🧾 Example
97+
```java
98+
public class Hello {
99+
public static void main(String[] args) {
100+
Greetings.sayHello();
101+
}
102+
}
103+
104+
public class Greetings {
105+
public static void sayHello() {
106+
System.out.println("Hello World...");
107+
}
108+
}
109+
```
110+
111+
▶️ Command
112+
```
113+
java Hello.java
114+
```
115+
116+
✅ Even though Hello depends on another class (Greetings), `Java 22` automatically detects and compiles both files before execution.
117+
118+
### 🧠 How It Works
119+
120+
| Java Version | Behavior |
121+
| ------------------ | ----------------------------------------------------------------------------- |
122+
| **Before Java 11** | Must compile manually using `javac` before running. |
123+
| **Java 11** | Can run a **single file** directly (`java Hello.java`). |
124+
| **Java 22** | Can run **multi-file source programs** — dependencies included automatically. |
125+
126+
### 💡 Key Benefits
127+
128+
- **Faster Prototyping :** No need to compile separately.
129+
130+
- **Simpler Workflow :** Run `.java` files directly like scripts.
131+
132+
- **Multi-File Support :** `Java 22` expands usability to larger projects.
133+
134+
- **Perfect for Demos, Learning, and Automation Scripts**.
135+
136+
### 🧾 Compilation & Execution Summary
137+
| Step | Before Java 11 | From Java 11 | From Java 22 |
138+
| --------------------------- | ------------------ | ------------------------- | ------------------------- |
139+
| **Compile** | `javac Hello.java` | *(Handled automatically)* | *(Handled automatically)* |
140+
| **Execute** | `java Hello` | `java Hello.java` | `java Hello.java` |
141+
| **Supports Multiple Files** ||||
142+
| **Ease of Use** | Manual | Simplified | Seamless |
143+
144+
## Compact Source Files & Instance Main Methods
145+
146+
The Compact Source Files and Instance Main Methods features introduced in Java 25 make Java simpler and more approachable for beginners, while still being powerful for experienced developers.
147+
148+
These features reduce boilerplate and let you start coding Java applications without needing to define classes or static methods.
149+
150+
### 💻 Traditional vs. Compact Example
151+
🧾 Before (Classic Java Program)
152+
```java
153+
public class HelloWorld {
154+
public static void main(String[] args) {
155+
System.out.println("Hello World!");
156+
}
157+
}
158+
```
159+
160+
✅ After (Compact Source File in Java 25)
161+
```java
162+
void main() {
163+
IO.println("Hello World!");
164+
}
165+
```
166+
167+
### 🧠 Explanation
168+
169+
- Compact Source Files remove the need for explicit class definitions in simple programs.
170+
- Instance Main Methods allow main() to be defined without the static keyword.
171+
- This keeps code concise, beginner-friendly, and ideal for quick prototypes or lightweight scripts.
172+
173+
💡 The Java compiler automatically creates an implicit top-level class for compact source files.
174+
175+
### ⚙️ Instance Main Method Explained
176+
177+
Traditionally, Java required main() to be static:
178+
```java
179+
180+
public static void main(String[] args) { }
181+
```
182+
183+
Now, in Java 25, the `main()` method can be non-static — the JVM automatically instantiates the implicit class to execute it.
184+
185+
🧾 Allowed Variations of main() in Compact Source Files
186+
```java
187+
void main() {}
188+
public void main() {}
189+
static void main() {}
190+
public static void main() {}
191+
public void main(String[] args) {}
192+
public static void main(String[] args) {}
193+
```
194+
🧩 If both a `main(String[] args)` and a no-argument `main()` exist, the JVM prefers `main(String[] args)` as the program’s entry point.
195+
196+
### 💬 Why It Matters
197+
198+
- Beginner-Friendly: No need to understand classes, objects, or static context before writing your first program.
199+
- Less Boilerplate: Write quick experiments, demos, or simple utilities faster.
200+
- Flexible for Experts: Enables rapid prototyping before scaling into larger applications.
201+
202+
203+
204+
### 🔖 Summary
205+
| Feature | Description |
206+
| ------------------------- | ------------------------------------------------- |
207+
| **Compact Source Files** | Write Java code without explicit class declarations. |
208+
| **Instance Main Methods** | Define `main()` without `static`. JVM handles instantiation. |
209+
| **Goal** | Simplify Java for beginners and enable quick prototyping. |
210+
| **Status** | Available in Java 25 |
211+
42212
Congratulations! You've successfully created and executed your first Java class using IntelliJ IDEA.

0 commit comments

Comments
 (0)