Note that there are some explanatory texts on larger screens.

plurals
  1. POService Loader implementations needing default public constructors
    primarykey
    data
    text
    <p>I was trying to understand how in Java Service Loader works? I came across this <a href="http://technotes.tostaky.biz/2013/03/java-serviceloader-example.html" rel="nofollow">blog</a>:</p> <p>Could you please help me understand why the author claims:</p> <blockquote> <p>The implementation must have a public parameterless constructor.</p> </blockquote> <p>Okay I get the first part. Now a follow up question. I can post it as a follow up a question but thought it would be better to have it as part of the same question. </p> <p>Consider the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.1-110-F.2" rel="nofollow">java doc</a>:</p> <p>It says:</p> <blockquote> <p>In addition, the constructor of a non-private inner member class must be compiled such that it has as its first parameter, an additional implicit parameter representing the immediately enclosing instance (§8.1.3). </p> </blockquote> <p>So does that mean I cannot externalize a inner class. Please consider the following code:</p> <pre><code>import java.io.*; class Y { class ABC { ABC() { System.out.println("ABC Constructor"); } public void writeExternal(ObjectOutput out) throws IOException { System.out.println("ABC.writeExternal"); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println("ABC.readExternal"); } } public void foo() throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); ABC abc = new ABC(); ObjectOutputStream o = new ObjectOutputStream( new FileOutputStream("InnerClass.out")); System.out.println("Saving objects:"); o.writeObject(abc); o.close(); // Now get them back: ObjectInputStream in = new ObjectInputStream( new FileInputStream("InnerClass.out")); System.out.println("Recovering abc:"); // OOPS! Throws an exception: abc = (ABC)in.readObject(); } } public class InnerClass { public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Hello World\n"); Y y = new Y(); System.out.println(y); y.foo(); } } </code></pre> <p>It fails at runtime:</p> <pre><code>Hello World Y@6bc7c054 Constructing objects: ABC Constructor Saving objects: Exception in thread "main" java.io.NotSerializableException: Y$ABC at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) at Y.foo(InnerClass.java:24) at InnerClass.main(InnerClass.java:40) </code></pre> <p>How can I use Externalize class ABC? </p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload