Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/semusings/in-5-minutes

In 5 Minutes
https://github.com/semusings/in-5-minutes

Last synced: 3 days ago
JSON representation

In 5 Minutes

Awesome Lists containing this project

README

        

---
marp: true
theme: your-theme
paginate: true
header: '© Software Engineering Musings - - subscribe us on '
---

section { overflow: auto; display: block; width: 100%; height: 100%; }

# Unnamed Classes and Instance Main Methods

**JEP-445**

- Ever found Java's "Hello, World!" program a bit too complex?

- Java's complexity can be daunting for beginners.

- No more unnecessary clutter in simple programs.

---

## Classic Approach

Classic Hello, World! program that is often used as the first program for Java students:

```java
public class HelloWorld {
static String greeting = "Hello, World!";

public static void main(String[] args) {
System.out.println(String.format(":%s\n:%s\n:%s", HelloWorld.class.getName(), greeting,
"Subscribe us on youtube.com/@semusings"));
}
}
```

```bash
java --source 22 --enable-preview HelloWorld.java
```

In this approach -> too much code, too many concepts, too many constructs

---

## Mordern Approach

Allow to lunch instance main methods. Such methods are not static, need not be public, and need not have a String[] parameter.

```java
class HelloWorldUsingInstanceMethod {

String greeting = "Hello, World!";

void main() {
System.out.println(String.format(":%s\n:%s\n:%s", getClass().getName(), greeting,
"Subscribe us on youtube.com/@semusings"));
}

}
```

```bash
java --source 22 --enable-preview HelloWorldUsingInstanceMethod.java
```

---

## Mordern Approach (contd.)

Unnamed classes to make the class declaration implicit

```java
String greeting = "Hello, World!";

void main() {
System.out.println(String.format(":%s\n:%s\n:%s", getClass().getName(), greeting,
"Subscribe us on youtube.com/@semusings"));
}
```

```bash
java --source 22 --enable-preview HelloWorldUsingUnnamedClass.java
```

---

## Thank You!

> Please feel free to reach out to me if you have any question.
>
> [email protected]

 
 
Subscribe us on YouTube

---

### References

-