Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Question on Enternalizable

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source : Javablackbelt.com


import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class ExternalizableClass {
public static void main(String...strings) throws IOException,ClassNotFoundException
{
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.ser"));
out.writeObject(tw);
ObjectInputStream input = new ObjectInputStream(new FileInputStream("tw.ser"));
TransientWriter tw2 = (TransientWriter) input.readObject();
System.out.println(tw2);
}

}
class TransientWriter implements Externalizable
{
private String s = "Hope i ever be persisted";

public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException {
s = (String) arg0.readObject();
}

public void writeExternal(ObjectOutput arg0) throws IOException {
arg0.writeObject(s);

}
public String toString()
{
return s;
}
}

I am getting Runtime exception

Exception in thread "main" java.io.InvalidClassException: Java.mockTests.javablackbelt.AbilaskKoneri.TransientWriter; no valid constructor
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Java.mockTests.javablackbelt.AbilaskKoneri.ExternalizableClass.main(ExternalizableClass.java:17)



I am sorry if this doen't cover on SCJP.

rami
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of TransientWriter MUST be :
public TransientWriter(){}

In your code you didn't declare any constructor, so the compiler put
a non-arg constructor with the same access modifier as the class :
so, it will put TransientWriter(){} (Default access !!!)
Remember: the constructor MUST be public
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks antonio.
That means , you are serializing an object , then its class constructor which accessed must be public. am i correct?

thanks
rami
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the class that implements Externalizable must have a public non-arg constructor.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!