• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Runtime API to find if the object is serialzable or not

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do we have any api which would tell us in runtime whether the object is serelizable or not?

something like this...

i am receiving object reference runtime which i am not aware whether is is serializable or not...

is there any way to find it out?


baseClassReference = ObjectReceivedByRuntime;


https://coderanch.com/t/417840/java-programmer-SCJP/certification/Serializable-issues
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the instanceof operator to determine if an object implements Serializable just as you would to determine class and inherited types.

Naturally you are depending on the designer of the class to use the Serializable marker interface correctly.

Bill
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


import java.io.InputStream;
import java.io.Serializable;
import java.sql.Connection;



class Base implements Serializable{

// assume this class is serializable

}

class Derived extends Base {

Connection con = null;
InputStream in =null;
// is thi class serializable too? noway... as input stream and connection are not serializable object...
// but System.out.println(b instanceof Serializable); == true;


}
public class TestSerializableSubClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Base b = new Derived();
System.out.println(b instanceof Serializable);

}

}


please advise ......
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Advise? Like I said - you depend on the designer to use Serializable correctly.

You could try writing the object to an ObjectOutputStream - an exception you could catch would be thrown.

Bill
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic