harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexey Varlamov" <alexey.v.varla...@gmail.com>
Subject Re: [classlib][lang] Is it possible to new an object without invoking any constructor?
Date Thu, 15 Nov 2007 05:26:17 GMT
2007/11/15, Andrew Zhang <zhanghuangzhu@gmail.com>:
> On Nov 15, 2007 1:07 PM, Nathan Beyer <ndbeyer@apache.org> wrote:
>
> > Do you mean this?
> >
> > class MyObject {
> >  String field;
> >  MyObject(){
> >      super();
> >  }
> > }
> >
> > MyObject mo = new MyObject();
> >
> > With regard to fields being in their default state, that's what
> > happens by default. The only thing that changes this is if you inline
> > field assigment with the declaration or assign the fields in the
> > constructor.
>
>
> Hi Nathan,
>
> I want an object with all null values for all reference fields, and default
> value(0/false) for primitive fields, even there's a constructor:
>
> public class MyObject {
>  int i;
>  String s;
>  B b;
>  MyObject() { i = 100; s = "hello";  b= new B(); }
> }
>
> I want to have a MyObject myObject whose i = 0, s = null and b = null.
> Yes, it's very like the initial state of de-serialized. What I have is the
> class definition (e.g class MyObject above), and I want to "new" an instance
> with all null/0 value of MyObject.class by some way. Is it possible? Thanks!

Only via reflection in general case. Deserialization would work only
if explicitly supported by the class, and no less tricky to use.
Moreover custom readObject() may reject nullified object, say, due to
constraints/invariant violation - no real difference to normal c-tor.

>
> >
> >
> > As for creating a new instance without calling a constructor, that's
> > what happens when an instance of something is de-serialized. No
> > constructor is called; just the readObject() and related methods are
>
>
> But I don't have an FileInputStream instance...
>
> >
> > invoked.
> >
> > -Nathan
> >
> > On Nov 14, 2007 11:58 PM, Andrew Zhang <zhanghuangzhu@gmail.com> wrote:
> > > Hi,
> > >
> > > Is it possible to new an object without invoking any constructor? I know
> > the
> > > question is a bit weird. What I want is to create an object with all
> > > null/default value for its fields.
> > > Reseting all fields to null by reflection is a possible solution, but is
> > > there any existing special method (no matter it's low level or Harmony
> > > specific) which can achieve this goal?
> > > Thanks!
> > >
> > >
> > > --
> > > Best regards,
> > > Andrew Zhang
> > >
> > > http://zhanghuangzhu.blogspot.com/
> > >
> >
>
>
>
> --
> Best regards,
> Andrew Zhang
>
> http://zhanghuangzhu.blogspot.com/
>

Mime
View raw message