<div dir="ltr">Also, a slightly Lispier way to do what you want:<div><br></div><div><font face="monospace">List.of(Foo.class, Bar.class, Baz.class)</font><span style="font-family:monospace">.stream()</span><span style="font-family:monospace">.map(c -> { </span></div><div><font face="monospace"> try {</font></div><div><font face="monospace"> return c.newInstance();</font></div><div><font face="monospace"> } catch(Exception e) { </font></div><div><font face="monospace"> e.printStackTrace(); </font></div><div><font face="monospace"> return null;</font></div><div><font face="monospace"> }</font></div><div><font face="monospace">}).</font><span style="font-family:monospace">collect(Collectors.toList());</span></div><div><br></div><div>The Stream API and lambdas make it almost pleasant to work with Java, but exception handling is still a bit tricky...</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 6 Feb 2021 at 21:06, Rudi Araújo <<a href="mailto:rudi.araujo@gmail.com">rudi.araujo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Class::newInstance() doesn't have any parameters (also, it's deprecated: better to use getConstructor() or getDeclaredConstructor() and call newInstance() on it).<div><br></div><div>Also, the generic agreement on your calls to makeAndPrint is failing.</div><div><br></div><div>This does what you wanted:</div><div><br></div><div><font face="monospace">jshell> class Foo { }<br>| modified class Foo<br><br>jshell> class Bar extends Foo { }<br>| modified class Bar<br><br>jshell> class Baz extends Foo { }<br>| modified class Baz<br><br>jshell> class Main {<br> ...> public static void main(String[] args) {<br> ...> makeAndPrint(Foo.class);<br> ...> makeAndPrint(Bar.class);<br> ...> makeAndPrint(Baz.class);<br> ...> }<br> ...><br> ...> public static void makeAndPrint(Class<? extends Foo> klass) {<br> ...> try {<br> ...> Object o = klass.newInstance();<br> ...> System.out.println("Created: " + o.toString());<br> ...> } catch (Exception e) {<br> ...> System.err.println("Failed: " + klass);<br> ...> }<br> ...> }<br> ...> }<br>| created class Main<br><br>jshell> Main.main(new String[]{})<br>Created: REPL.$JShell$11$Foo@6a4f787b<br>Created: REPL.$JShell$12$Bar@7d9d1a19<br>Created: REPL.$JShell$13$Baz@39c0f4a<br></font></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 6 Feb 2021 at 20:56, Luís Oliveira <<a href="mailto:luismbo@gmail.com" target="_blank">luismbo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, 6 Feb 2021 at 16:29, Manfred Bergmann <<a href="mailto:manfred.bergmann@me.com" target="_blank">manfred.bergmann@me.com</a>> wrote:<br>
> But this is kind of also possible in i.e. Java where you can say:<br>
> Foo.class.newInstance() which uses reflection and is not normally used.<br>
> But is effectively similar to make-instance `foo.<br>
<br>
My Java is getting rustier and rustier, but maybe you can tell me why<br>
the following program doesn't compile:<br>
<br>
class Foo { }<br>
class Bar extends Foo { }<br>
class Baz extends Foo { }<br>
<br>
class Main {<br>
public static void main(String[] args) {<br>
makeAndPrint(Foo.class);<br>
makeAndPrint(Bar.class);<br>
makeAndPrint(Baz.class);<br>
}<br>
<br>
public static void makeAndPrint(Class<Foo> klass) {<br>
try {<br>
Object o = klass.newInstance(klass);<br>
System.out.println("Created: " + o.toString());<br>
} catch (Exception e) {<br>
System.err.println("Failed: " + klass);<br>
}<br>
}<br>
}<br>
<br>
The rough equivalent in Common Lisp would be:<br>
<br>
(defclass foo () ())<br>
(defclass bar (foo) ())<br>
(defclass baz (foo) ())<br>
<br>
(mapcar #'make-instance '(foo bar baz))<br>
<br>
Luís<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><font size="1">He who binds to himself a joy<br>Doth the winged life destroy<br>But he who kisses the joy as it flies<br>Lives in Eternitys sun rise<br><br>- William Blake</font></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><font size="1">He who binds to himself a joy<br>Doth the winged life destroy<br>But he who kisses the joy as it flies<br>Lives in Eternitys sun rise<br><br>- William Blake</font></div>