0% found this document useful (0 votes)
1 views

Java21 Unnamed Classes and Instance Main Methods

Java 21 introduces unnamed classes and instance main methods to simplify the learning process for beginners by reducing the complexity associated with traditional programming constructs. Unnamed classes are designed for standalone programs and cannot implement interfaces, while instance main methods eliminate the need for static and public modifiers. This approach allows learners to focus on basic programming without the overhead of large program structures.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java21 Unnamed Classes and Instance Main Methods

Java 21 introduces unnamed classes and instance main methods to simplify the learning process for beginners by reducing the complexity associated with traditional programming constructs. Unnamed classes are designed for standalone programs and cannot implement interfaces, while instance main methods eliminate the need for static and public modifiers. This approach allows learners to focus on basic programming without the overhead of large program structures.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java 21

Unnamed Classes and Instance Main Methods

Copyright © Seán Kennedy


Unnamed Classes and Instance Main Methods
• This is a preview feature.
• https://openjdk.org/jeps/445

Copyright © Seán Kennedy


Unnamed Classes and Instance Main Methods

• Java supports both “programming in the small” (variables,


methods, control flow etc.. ) and “programming in the large”
(classes, interfaces, packages, modules etc..).

• The goal is to focus on the “programming in the small” by


reducing ceremony/scaffolding for those learning the
language.

• Constructs such as classes, access modifiers such as public


and keywords such as static relate to “programming in the
large” and should only be encountered when required.
3

Copyright © Seán Kennedy


Unnamed Classes and Instance Main Methods

• Java supports both “programming in the small” (variables,


methods, control flow etc.. ) and “programming in the large”
(classes, interfaces, packages, modules etc..).

• The goal is to focus on the “programming in the small” by


reducing ceremony/scaffolding for those learning the
language.

• Constructs such as classes, access modifiers such as public


and keywords such as static relate to “programming in the
large” and should only be encountered when required.
4

Copyright © Seán Kennedy


Unnamed Classes and Instance Main Methods

• In effect, make Java easier to learn. To this end, JEP 445


enables learners to write their first programs without needing
to understand language features designed for large programs.

• Basic programs in a concise manner.

Copyright © Seán Kennedy


Instance Main Methods

• Instance main methods:


• no need for static, public or a String [] parameter

• If you have both the traditional public static void


main(String[] args) and the instance main(), the traditional
version takes precedence.

Copyright © Seán Kennedy


Unnamed Classes

• Unnamed classes:
• extend from Object and cannot implement an interface
• are final and reside in the unnamed package
• their .class name on the hard disk depends on the
filename – for example, if the above code
is in HelloWorld.java, HelloWorld.class is created on
the hard disk

Copyright © Seán Kennedy


Unnamed Classes

• Unnamed classes:
• are exactly like normal classes except that an unnamed
class has only one constructor – the default no-args
constructor provided by the compiler.
• it is an error to explicitly code a constructor, even a
no-args constructor.
• the this keyword is still valid.

Copyright © Seán Kennedy


Unnamed Classes

• Unnamed classes:
• as code cannot refer to an unnamed class by name,
instances of an unnamed class cannot be constructed
directly.
• therefore, such classes are useful for standalone programs
or as an entry-point to a program.
• as a result, unnamed classes must have a main() method.

Copyright © Seán Kennedy


Unnamed Classes

10

Copyright © Seán Kennedy

You might also like