New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes (Preview)

Cay Horstmann cay.horstmann at gmail.com
Tue Apr 11 06:13:02 UTC 2023


I agree with Ken that this is a helpful step. It is particularly useful for an "objects early" approach, which plays to one of Java's strengths vis-a-vis Python.

I am a little concerned what I would do in my CS1 textbooks in the transitional period where adopters and students may use a mixture of older and newer Java versions.

Adopters would also have to get used to the absence of "public" in early programs. I think that is a good idea, but I can feel the initial resistance.

I suppose I could set "public" and "static" in light gray for a while.

I agree that System.out.println is a problem. I always am troubled by having to explain, in Chapter 2, the difference between

1. Scanner in = new Scanner(System.in); // static variable in constructor
2. int n = in.nextInt(); // instance method
3. double x = Math.sqrt(n); // static method
3. System.out.println(...); // instance method invoked on static variable

One way of addressing that problem would be an App class with methods nextInt, println, sqrt, and so on, which an anonymous class automatically extends. But that can't be retrofitted to this JEP at a later date. For that reason, I would like to see the proposed solution for beginner-friendly console I/O.

Cheers,

Cay


On 11/04/2023 02.00, Ken Fogel wrote:
> As someone who has lamented the decorations necessary to say Hello World, I am excited about this JEP. Python is eating our lunch because it supports the stream of consciousness Design pattern, make it work and then maybe make it nice.
> 
> That is not to say this simplification for new users won’t lead to the same poor design pattern, a good instructor or good book can point out that this new style is about understanding syntax and then methods. From here you can move to OOP or Functional in Java.
> 
> I applaud this new JEP.
> 
> Ken Fogel
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> *From:* jdk-dev <jdk-dev-retn at openjdk.org> on behalf of Brian Goetz <brian.goetz at oracle.com>
> *Sent:* Tuesday, April 11, 2023 7:24:24 AM
> *To:* Attila Kelemen <attila.kelemen85 at gmail.com>
> *Cc:* amber-dev at openjdk.org <amber-dev at openjdk.org>; Ron Pressler <ron.pressler at oracle.com>; jdk-dev at openjdk.org <jdk-dev at openjdk.org>
> *Subject:* Re: New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes (Preview)
> As you allude to, there is already the “Single File Source Launcher”, which let’s you run a Java program directly from the command line without compilation (`java Foo.java`), which has been around for a while, and additionally does support “shebang” execution.  (There are plans to extend this further.). This was aimed at script-like programs, which, while not written by beginners, also do not require the ceremony of the class declaration.  So the design center here is not “beginning programmers”, but “simple programs”, but of course there is a lot of intersection between the two — beginning programmers write simple programs.
> 
> There is also a chicken-and-egg problem here; one of the reasons people don’t write script-like programs in Java so often is because of the declaration and building overhead.  But that’s an accidental consequence, and we can remove these impediments.  So students may be the most obvious group of beneficiaries, but they are not the only ones.
> 
> 
> 
>> On Apr 10, 2023, at 11:45 AM, Attila Kelemen <attila.kelemen85 at gmail.com> wrote:
>> 
>> Sure, sorry, didn't want to elaborate too long, and lose the main
>> message (thus only the assertion), but here is my problem in more
>> detail:
>> 
>> The reason why I don't think it would be useful in general, because it
>> only makes writing entry points easier, which would give more
>> incentive to "#!/bin/java" (correct me if I'm wrong, but I vaguely
>> remember that there is a JEP for java to auto compile java files and
>> run them), but I find that to be optimistic, since if somebody wants
>> something like that, the small extra boilerplate won't stop them. So,
>> basically this JEP provides a minimal simplification of writing an
>> entry point, and entry points are rare (compared to the rest of the
>> code). This is merely my opinion, but writing out the small boiler
>> plate for the entry point didn't bother me at all. Thus, I would say
>> that this JEP provides little benefit to anyone but beginners. Or
>> rather, I would throw back the question: What could I gain from using
>> this? What value do I get from this as a non-beginner? (not trying to
>> mock you, I'm honestly curious what am I missing here) I did see you
>> mention on another list that abstract classes could have a main
>> method, but if we want such a thing, that need not be tied to this
>> JEP, and we shouldn't prevent future possibilities by introducing this
>> JEP (such as easier writing of utility classes).
>> 
>> Brian Goetz <brian.goetz at oracle.com> ezt írta (időpont: 2023. ápr.
>> 10., H, 20:17):
>>> 
>>>> While I like the idea of making the language more beginner friendly, I
>>>> don't think the proposal in this form is the best way, because it does
>>>> exactly what it claims to want to avoid: It introduces a separate
>>>> beginner's dialect. That is, a form of writing code only to be used by
>>>> beginners and never again.
>>> 
>>> Since you provide no argument or examples to support your assertion, it is pretty hard to tell where you’ve gone off the trail here.
>>> 
>>> But your claim that the features here are useful only to beginners and never again is simply incorrect.  If you want to unpack your claim, perhaps we can clarify the disconnect.
>>> 
>>> 
> 

-- 

--

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the jdk-dev mailing list