New candidate JEP: 477: Implicitly Declared Classes and Instance Main Methods (Third Preview)

Brian Goetz brian.goetz at oracle.com
Wed May 8 14:10:44 UTC 2024



On 5/7/2024 3:06 PM, Olexandr Rotan wrote:
> Could it be possible to not oblige users to create a void main() 
> method? It would be great if a person that sees java for the 
> first time has the option to write code in plain document without any 
> additional method declarations (even method concept is kind of hard 
> for a complete newbie).

This direction ("just let me mix statements and variables at the top 
level, and let the compiler figure it out") was extensively explored 
during the design process, for the obvious reasons, but it turned out 
that this falls afoul of the project goals in a pretty big way, so it 
was rejected.

> I see this accomplishable without any changes to Java language in a 
> following way:

I think you may be confused on what "Java Language" means.  You talk 
about "compiler transformations" as if they are outside the language, 
but the compiler *implements* the language, as specified in the 
specification.  The compiler has no latitude to accept programs that are 
not given meaning by the specification.  (To think otherwise is a fatal 
type error.)

> 1. Every variable declaration is elevated to the top-level scope of 
> implicitly declared class without initialization. Initialization will 
> be performed later. There could possibly be a problem with var 
> keyword, but if desugaring is performed after attribution, type idents 
> could be explicitly added in the "trans" phase.
> 2. Every other top-level statement that is not a method or class 
> declaration in a document is wrapped in the anonymous code block of an 
> implicitly declared class. In this scope any "this" refs should be 
> forbidden as well as return statements.
> 3. Every top-level class and method declaration is elevated to a 
> top-level declaration inside an implicitly declared class. If a class 
> is not static, any instance creation of such classes after desugaring 
> should be created as this.new X(). Constructor declarations for 
> implicitly declared class must be forbidden.
> 4. Main method is implicitly generated. It creates a new instance of 
> implicitly declared class. This will trigger run of code in anonymous 
> code blocks.
>
> I understand that these changes could dramatically change current flow 
> in the implementation, but this way I think this feature could be more 
> friendly towards new users by allowing them to write their first hello 
> world program without having to learn concepts of methods and possibly 
> confusing concepts of void type.
>
> Best regards.
>
> On Tue, May 7, 2024 at 8:55 PM Mark Reinhold 
> <mark.reinhold at oracle.com> wrote:
>
>     https://openjdk.org/jeps/477
>
>       Summary: Evolve the Java programming language so that beginners can
>       write their first programs without needing to understand language
>       features designed for large programs. Far from using a separate
>       dialect of the language, beginners can write streamlined
>     declarations
>       for single-class programs and then seamlessly expand their
>     programs to
>       use more advanced features as their skills grow. Experienced
>     developers
>       can likewise enjoy writing small programs succinctly, without
>     the need
>       for constructs intended for programming in the large. This is a
>     preview
>       language feature.
>
>     - Mark
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20240508/c404ec76/attachment-0001.htm>


More information about the jdk-dev mailing list