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

Cay Horstmann cay.horstmann at gmail.com
Tue Apr 11 07:00:46 UTC 2023



On 11/04/2023 08.41, Remi Forax wrote:
> 
> 
> ----- Original Message -----
>> From: "cay horstmann" <cay.horstmann at gmail.com>
>> To: "jdk-dev" <jdk-dev at openjdk.org>
>> Sent: Tuesday, April 11, 2023 8:13:02 AM
>> Subject: Re: New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes (Preview)
> 
>> ...
>> 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.
> 
> yes, a class "IO" is the next step
> 
>    int number = IO.scanInt();
>    IO.log(number);
> 
> with the idea that maybe there is an "import static java.lang.IO.*;" by default (if it doe not clash with other explicit static imports) so the code can be simplified to
>    
>    int number = scanInt();
>    log(number);
> 
I am fine with a class IO with static methods since I already need to teach static Math methods. It would be helpful if the method names were the same as for Scanner and PrintStream, i.e. nextInt, nextDouble, nextLine, next, println, print, printf.

I don't think default imports are necessary. Whenever I suggested to use import static java.lang.Math.*; instructors stared at me blankly. There seemed no objection to Math.sqrt, so IO.println would probably be ok as well.

Cheers,

Cay

--

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


More information about the jdk-dev mailing list