Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36669 > unrolled thread

How to assign an object to another in it method?

Started byparmenides <xparmenides@yahoo.com>
First post2017-06-18 20:02 +0800
Last post2017-06-19 13:40 -0700
Articles 20 on this page of 68 — 10 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to assign an object to another in it method? parmenides <xparmenides@yahoo.com> - 2017-06-18 20:02 +0800
    Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-18 08:09 -0400
    Re: How to assign an object to another in it method? Robert Klemme <shortcutter@googlemail.com> - 2017-06-18 15:31 +0200
    Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 10:22 -0400
      Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 08:13 -0700
        Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 11:53 -0400
          Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 09:06 -0700
            Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-19 13:56 -0400
        Re: How to assign an object to another in it method? lewbloch@gmail.com - 2017-06-27 13:49 -0700
          Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-06-28 12:20 +0000
            Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-28 05:51 -0700
              Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-28 14:03 -0400
              Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-02 21:23 -0400
                Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 10:59 +0000
                  Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-03 08:38 -0400
                    Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-03 14:04 +0000
                      Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 10:39 -0400
                        Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-03 16:28 +0000
                          Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 16:52 -0400
                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-03 22:04 -0400
                        Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-03 23:15 -0400
                          Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-04 21:46 -0400
                            Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-04 22:18 -0400
                              Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-08 21:13 -0400
                                Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-08 22:12 -0400
                                  Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-11 17:56 +0000
                                    Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 15:40 -0400
                                      Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-11 13:49 -0700
                                        Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 17:41 -0400
                                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:27 -0400
                                        Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:43 -0400
                                          Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:49 -0400
                                            Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-12 17:20 -0400
                                              Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 10:08 +0000
                                                Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-13 11:35 +0000
                                                  Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 08:14 -0400
                                                    Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-13 14:29 +0000
                                                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 13:47 -0400
                                                        Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 18:09 +0000
                                                          Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 15:01 -0400
                                                            Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 19:19 +0000
                                                              Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 15:39 -0400
                                                  Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 16:49 +0000
                                                Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-13 10:24 -0400
                                                  Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-13 17:19 +0000
                                                    Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 21:17 -0400
                                                      Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-14 06:40 +0000
                                                        Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 14:26 -0400
                                                          Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-15 14:36 +0000
                                                    Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-14 12:10 -0400
                                                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 14:40 -0400
                                                        Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-14 14:51 -0400
                                                          Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 21:05 -0400
                                                        Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-14 21:06 -0400
                                            Re: How to assign an object to another in it method? lewbloch@gmail.com - 2017-07-14 14:44 -0700
                                          Re: How to assign an object to another in it method? Andreas Leitgeb <avl@logic.at> - 2017-07-12 16:23 +0000
                                            Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 10:12 -0700
                                  Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:16 -0400
                                    Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:46 -0400
                                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:52 -0400
                                  Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 20:24 -0400
                                    Re: How to assign an object to another in it method? Jerry Stuckle <jstucklex@attglobal.net> - 2017-07-11 21:49 -0400
                                      Re: How to assign an object to another in it method? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-11 21:55 -0400
                  Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 12:37 +0000
                    Re: How to assign an object to another in it method? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-03 11:34 -0400
                      Re: How to assign an object to another in it method? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-03 17:28 +0000
    Re: How to assign an object to another in it method? v_borchert@despammed.com (Volker Borchert) - 2017-06-19 17:13 +0000
    Re: How to assign an object to another in it method? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-19 13:40 -0700

Page 1 of 4  [1] 2 3 4  Next page →


#36669 — How to assign an object to another in it method?

Fromparmenides <xparmenides@yahoo.com>
Date2017-06-18 20:02 +0800
SubjectHow to assign an object to another in it method?
Message-ID<oi5q4t$1okn$1@gioia.aioe.org>
Hi,

    Suppose that there is a class 'Foo' and an its instance called 'f1'. 
I want to assign 'f1' to another instance called 'f2' within a method 
invoked by f2. The code like this:

    Class Foo {
	... ...
	public someMethod() {
	    Foo f1 = new Foo();
	    f1.readFromDB(); 	// Now I get an instance
	    this.XXX = f1.getXXX();
	    this.YYY = f1.getYYY();
	    ... ...
	}
    }
    ... ...
    Foo f2 = new Foo();
    f2.someMethod();

That is I want to read data from Database, and instanciate an object 
from these data. Then, the object is assigned to another object. In 
someMethod(), the feasible way is to assign the member variables one by 
one. But, I think this is cumbersome at all. So I try to do the 
assignment as a whole like this:
	this = f1;
and write a clone() method to support this kind of assignment. However, 
owing to 'this' is just an reference to 'f2' rather than an object, the 
compiler does not agree. Is there any way to do assigment as a whole in 
this situation? Thanks.

[toc] | [next] | [standalone]


#36670

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-06-18 08:09 -0400
Message-ID<oi5qai$s23$1@dont-email.me>
In reply to#36669
On 6/18/2017 8:02 AM, parmenides wrote:
> Hi,
> 
>     Suppose that there is a class 'Foo' and an its instance called 'f1'. 
> I want to assign 'f1' to another instance called 'f2' within a method 
> invoked by f2. The code like this:
> 
>     Class Foo {
>      ... ...
>      public someMethod() {
>          Foo f1 = new Foo();
>          f1.readFromDB();     // Now I get an instance
>          this.XXX = f1.getXXX();
>          this.YYY = f1.getYYY();
>          ... ...
>      }
>     }
>     ... ...
>     Foo f2 = new Foo();
>     f2.someMethod();
> 
> That is I want to read data from Database, and instanciate an object 
> from these data. Then, the object is assigned to another object. In 
> someMethod(), the feasible way is to assign the member variables one by 
> one. But, I think this is cumbersome at all. So I try to do the 
> assignment as a whole like this:
>      this = f1;
> and write a clone() method to support this kind of assignment. However, 
> owing to 'this' is just an reference to 'f2' rather than an object, the 
> compiler does not agree. Is there any way to do assigment as a whole in 
> this situation? Thanks.

     Why not simply

	public void someMethod() {
	    this.readFromDB();  // "this." could be omitted
	}

?  Then you can call someMethod() on as many different Foo instances
as you desire:

	Foo f1 = new Foo();
	f1.someMethod();

	Foo f2 = new Foo();
	f2.someMethod();

	Foo[] foos = new Foo[42];
	for (int i = 0; i < foos.length; ++i) {
	    foos[i] = new Foo();
	    foos[i].someMethod();
	}

	...

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred twelve days to go.

[toc] | [prev] | [next] | [standalone]


#36671

FromRobert Klemme <shortcutter@googlemail.com>
Date2017-06-18 15:31 +0200
Message-ID<eqndmgFs4e9U1@mid.individual.net>
In reply to#36669
On 18.06.2017 14:02, parmenides wrote:

>     Suppose that there is a class 'Foo' and an its instance called 'f1'. 
> I want to assign 'f1' to another instance called 'f2' within a method 
> invoked by f2. The code like this:
> 
>     Class Foo {
>      ... ...
>      public someMethod() {
>          Foo f1 = new Foo();
>          f1.readFromDB();     // Now I get an instance
>          this.XXX = f1.getXXX();
>          this.YYY = f1.getYYY();
>          ... ...
>      }
>     }
>     ... ...
>     Foo f2 = new Foo();
>     f2.someMethod();

If I understand correctly you have an instance method that reads state 
of an object from a database.  Then you want to assign that state to 
another instance.  But what for?  You could just keep it in the object 
that it was read into.

> That is I want to read data from Database, and instanciate an object 
> from these data.

This is not what the code shows.  The object is instantiated with a 
default constructor and - we have to assume - readFromDB() updates state 
of that object, presumably from a DB query result.

> Then, the object is assigned to another object. In 

You can only assign object references to variables.  What you seem to 
mean here is that you want to copy state from one instance to another. 
Note that depending on what Foo's state contains you would need to make 
sure you do not introduce errors via aliasing.

> someMethod(), the feasible way is to assign the member variables one by 
> one. But, I think this is cumbersome at all. So I try to do the 
> assignment as a whole like this:
>      this = f1;
> and write a clone() method to support this kind of assignment. However, 
> owing to 'this' is just an reference to 'f2' rather than an object, the 
> compiler does not agree. Is there any way to do assigment as a whole in 
> this situation? Thanks.

I think your design could be better.  You just should make someMethod() 
static and have it return the new instance.   Copying around does not 
make sense since f1 is lost after the method call anyway so the copying 
is just wasting CPU cycles.  So, just do:

class Foo {
   public static Foo newFromDB() {
     final Foo f = new Foo();
     f.readFromDB();
     return f;
   }
}

Foo f2 = Foo.newFromDB();

Btw. where does the DB context come from?  Apparently you are 
transporting it via some non obvious way (global variable, static 
member, thread local).  This is quite non obvious and may impose some 
obstacles to testing.  Also, there could be multithreading issues in 
some of these cases.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#36674

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-06-19 10:22 -0400
Message-ID<oi8mfs$ies$1@dont-email.me>
In reply to#36669
On 6/18/2017 8:47 PM, Stefan Ram wrote:
> parmenides <xparmenides@yahoo.com> writes:
>> Is there any way to do assigment as a whole in this
>> situation?
> 
>    The constructor can read the fields from the database:
> 
> class Alpha
> {
>    public Alpha()
>    { /* read the fields from the database here */ }}

     Although this is possible, it is often ill-advised to "do too
much work" in a constructor.  One drawback is the difficulty in
extending such a class:

	public class Beta extends Alpha {
	   public Beta() {
	       ...
	   }
	}

The problem here is that when a Beta is under construction, the
database activity occurs before the Beta instance is "ready."
That *may* be acceptable, but can also be dangerous.  I'd suggest
that if you do "a lot" of work in a constructor, you should make
sure the class cannot be extended: make it `final', or make all
the constructors `private'.

>    . Sometimes, a factory method might be more appropriate.

     Agreed.  Another approach is to use an initialization method:

	public class Alpha {
	    public Alpha() {
	        // Do only a little work here
	    }
	    public init() {
	        // Read from the database
	    }
	    ...
	}

This can become a bit clumsy, though: You have to make sure that
init() is called exactly once, which means you probably need a
`private boolean hasBeenInitialized;' data member that most
other methods must test so they can throw IllegalStateException
if anything is amiss.  Usually, Stefan's suggestion of a static
factory method calling a private constructor works more smoothly
and produces less clutter in the code.

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred eleven days to go.

[toc] | [prev] | [next] | [standalone]


#36675

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-06-19 08:13 -0700
Message-ID<b0821ba8-d4ab-49e9-980f-3d2c772bfd9f@googlegroups.com>
In reply to#36674
On Monday, June 19, 2017 at 10:22:27 AM UTC-4, Eric Sosman wrote:
>      Although this is possible, it is often ill-advised to "do too
> much work" in a constructor.  One drawback is the difficulty in
> extending such a class:

Well, anything in a constructor should only be in one constructor and should only be code that needs to execute any time you create a new instance of that class or any subclass, then subclasses should be required to call the super in their constructor.  This is not a big deal in a protected class.  Now if someone else might extend your class and it's going to be a big hassle if they don't call that super constructor it may be best to disable all constructors and use a factory.

[toc] | [prev] | [next] | [standalone]


#36677

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-06-19 11:53 -0400
Message-ID<oi8rqb$luc$1@dont-email.me>
In reply to#36675
On 6/19/2017 11:13 AM, Eric Douglas wrote:
> On Monday, June 19, 2017 at 10:22:27 AM UTC-4, Eric Sosman wrote:
>>       Although this is possible, it is often ill-advised to "do too
>> much work" in a constructor.  One drawback is the difficulty in
>> extending such a class:
> 
> Well, anything in a constructor should only be in one constructor and should only be code that needs to execute any time you create a new instance of that class or any subclass, then subclasses should be required to call the super in their constructor.  This is not a big deal in a protected class.  Now if someone else might extend your class and it's going to be a big hassle if they don't call that super constructor it may be best to disable all constructors and use a factory.
> 

     I'm not sure I understand you.  Subclass constructors *are* required
to call a constructor of their direct superclass.  There's no "should"
about it: The compiler enforces the requirement, and I believe the JVM
does so as well.

     Also, I don't see how anyone can "extend your class" if you "disable
all constructors" (which, I assume, means make them all `private').  In
any case, an extending class *cannot* simply forget to call a superclass
constructor.

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred eleven days to go.

[toc] | [prev] | [next] | [standalone]


#36678

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-06-19 09:06 -0700
Message-ID<044e754b-1259-4084-8891-dfef43b37ad4@googlegroups.com>
In reply to#36677
On Monday, June 19, 2017 at 11:53:20 AM UTC-4, Eric Sosman wrote:
>      I'm not sure I understand you.  Subclass constructors *are* required
> to call a constructor of their direct superclass.  There's no "should"
> about it: The compiler enforces the requirement, and I believe the JVM
> does so as well.
> 
>      Also, I don't see how anyone can "extend your class" if you "disable
> all constructors" (which, I assume, means make them all `private').  In
> any case, an extending class *cannot* simply forget to call a superclass
> constructor.

I guess I haven't created new subclasses lately.  So yeah, no problem with putting necessary initialization in the constructor as long as subclasses are required to call that constructor, just put it in one constructor and make any other constructors call that one.

The advantage I see to making all constructors private is if you think the constructor parameters might change, you can build that into the factory?  Otherwise the factory is only required if you're dynamically generating classes with an interface.

[toc] | [prev] | [next] | [standalone]


#36679

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-06-19 13:56 -0400
Message-ID<oi931m$om6$1@dont-email.me>
In reply to#36678
On 6/19/2017 12:06 PM, Eric Douglas wrote:
> On Monday, June 19, 2017 at 11:53:20 AM UTC-4, Eric Sosman wrote:
>>       I'm not sure I understand you.  Subclass constructors *are* required
>> to call a constructor of their direct superclass.  There's no "should"
>> about it: The compiler enforces the requirement, and I believe the JVM
>> does so as well.
>>
>>       Also, I don't see how anyone can "extend your class" if you "disable
>> all constructors" (which, I assume, means make them all `private').  In
>> any case, an extending class *cannot* simply forget to call a superclass
>> constructor.
> 
> I guess I haven't created new subclasses lately.  So yeah, no problem with putting necessary initialization in the constructor as long as subclasses are required to call that constructor, just put it in one constructor and make any other constructors call that one.

     Just so everyone's clear: A subclass constructor *must* call a
constructor of its immediate superclass.  The call can be indirect
(via a chain of subclass constructors delegating to each other by
`this()' calls), but a superclass constructor *must* be called.
Java insists on this, and won't let you leave it out.

     However, my point was that it's usually a mistake for any
constructor to do "a lot" of work.  For example, it is usually not
a good idea for a constructor to start threads, to register itself
as an event listener, and so on -- anything that might "leak" the
`this' reference before the entire chain of constructors finishes
is trouble waiting to happen, especially in an extensible class.

> The advantage I see to making all constructors private is if you think the constructor parameters might change, you can build that into the factory?  Otherwise the factory is only required if you're dynamically generating classes with an interface.

     If the constructors' parameters change, one supposes that the
factory methods' parameter lists would also change.  Shielding the
users from a constructor change while inflicting factory changes
upon them doesn't seem a tremendous benefit.

     Straying a little, it sometimes happens that a constructor (or
factory method) needs "too many" parameters in order to initialize a
fully-operational object instance.  One way to deal with this is to
take parameters for the "they'll certainly want to specify this" parts
of the state and provide setXxx() methods for the "these will usually
not be changed" bits -- consider the setEnabled() methods on various
Swing components, for example.  This is fine (although it can lead to
a plethora of tiny methods), but it means the settable parts of the
state can't be immutable.  Immutability is often a benefit, so what
is one to do?

     An attractive solution is the "builder" pattern.  You make a
ThingBuilder class with lots of setXxx() methods:

	public class ThingBuilder() {
	    private Which what;
	    private double trouble;
	    ...
	    public setWhat(Which what) {this.what = what;}
	    public Which getWhat() {return what;}
	    public setTrouble(double trouble) {this.trouble = trouble;}
	    public double getTrouble() {return trouble;}
	    ...
	}

... and then an immutable Thing class with just one constructor and
lots of `final' fields:

	public class Thing {
	    private final Which what;
	    private final double trouble;
	    ...
	    public Thing(ThingBuilder builder) {
	        this.what = builder.getWhat();
	        this.trouble = builder.getTrouble();
	        ...
	    }
	}

This may look like a lot of useless running around, but when the state
of a Thing is large and you'd like (much of) it to be immutable, the
pattern can be quite useful.

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred eleven days to go.

[toc] | [prev] | [next] | [standalone]


#36744

Fromlewbloch@gmail.com
Date2017-06-27 13:49 -0700
Message-ID<bdd41c94-f8b7-4c7d-869f-5375e68da69e@googlegroups.com>
In reply to#36675
Eric Douglas wrote:
> Well, anything in a constructor should only be in one constructor 

Not necessarily. Constructors can chain through each other, so things in one constructor can be in many constructors. Instance initializer blocks by definition are part of all constructors for their class, so those aren't only in one constructor either.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#36749

FromAndreas Leitgeb <avl@logic.at>
Date2017-06-28 12:20 +0000
Message-ID<slrnol7802.o2q.avl@logic.at>
In reply to#36744
lewbloch@gmail.com <lewbloch@gmail.com> wrote:
> Eric Douglas wrote:
>> Well, anything in a constructor should only be in one constructor 
> Not necessarily. Constructors can chain through each other, so things
> in one constructor can be in many constructors.

It seems to me like you're both in violent agreement here, except for
the nomenclature of what is "in a constructor".

Is that what is lexically in the respective c'tor's body, or does it also
include other c'tors called via this(...)?  Would it then also include
private, final or static methods called from some c'tor?

Obviously, stuff shouldn't be exactly duplicated in multiple c'tor
bodies, and doesn't need to, either, for reasons stated.

[toc] | [prev] | [next] | [standalone]


#36750

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-06-28 05:51 -0700
Message-ID<46334961-cc18-491f-828c-32e7401c7832@googlegroups.com>
In reply to#36749
On Wednesday, June 28, 2017 at 8:24:54 AM UTC-4, Andreas Leitgeb wrote:
> Obviously, stuff shouldn't be exactly duplicated in multiple c'tor
> bodies, and doesn't need to, either, for reasons stated.

public class MyClass
String var1;
public MyClass(){
this("init");
}
public MyClass(final String arg){
var1=arg;
}

Normally it's like this.  Now if you were to put something in the no arg constructor and something calls this one arg constructor with a super(), that gets missed.  I can't think of a case where you would want 2 constructors doing something totally different that shouldn't actually be 2 different classes.

[toc] | [prev] | [next] | [standalone]


#36754

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-06-28 14:03 -0400
Message-ID<oj0qr4$2dv$1@dont-email.me>
In reply to#36750
On 6/28/2017 8:51 AM, Eric Douglas wrote:
> [...]  I can't think of a case where you would want 2 constructors doing something totally different that shouldn't actually be 2 different classes.

	List<Thing> list1 = new ArrayList<>();
	List<Thing> list2 = new ArrayList<>(thingCollection);

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred two days to go.

[toc] | [prev] | [next] | [standalone]


#36792

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-07-02 21:23 -0400
Message-ID<ojc6ac$i54$1@gioia.aioe.org>
In reply to#36750
On 6/28/2017 8:51 AM, Eric Douglas wrote:
> On Wednesday, June 28, 2017 at 8:24:54 AM UTC-4, Andreas Leitgeb wrote:
>> Obviously, stuff shouldn't be exactly duplicated in multiple c'tor
>> bodies, and doesn't need to, either, for reasons stated.
> 
> public class MyClass
> String var1;
> public MyClass(){
> this("init");
> }
> public MyClass(final String arg){
> var1=arg;
> }
> 

> Normally it's like this.  Now if you were to put something in the no
> arg constructor and something calls this one arg constructor with a
> super(), that gets missed.  I can't think of a case where you would
> want 2 constructors doing something totally different that shouldn't
> actually be 2 different classes. >

The above is one reason - which is really the issue of Java formal
parameters not supporting default values.

But there are other reasons. One that comes to my mind
is different input types carrying the same information.

String(String original)
String(char[] value)
String(byte[] bytes, String charsetName)
etc.

comes to mind.

Of course those are also tightly related.

Arne

[toc] | [prev] | [next] | [standalone]


#36793

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2017-07-03 10:59 +0000
Message-ID<ojd820$gl5$1@dont-email.me>
In reply to#36792
On Sun, 02 Jul 2017 21:23:26 -0400, Arne Vajhøj wrote:

> On 6/28/2017 8:51 AM, Eric Douglas wrote:
>> On Wednesday, June 28, 2017 at 8:24:54 AM UTC-4, Andreas Leitgeb wrote:
>>> Obviously, stuff shouldn't be exactly duplicated in multiple c'tor
>>> bodies, and doesn't need to, either, for reasons stated.
>> 
>> public class MyClass String var1;
>> public MyClass(){
>> this("init");
>> }
>> public MyClass(final String arg){
>> var1=arg;
>> }
>> 
>> 
>> Normally it's like this.  Now if you were to put something in the no
>> arg constructor and something calls this one arg constructor with a
>> super(), that gets missed.  I can't think of a case where you would
>> want 2 constructors doing something totally different that shouldn't
>> actually be 2 different classes. >
> 
> The above is one reason - which is really the issue of Java formal
> parameters not supporting default values.
> 
> But there are other reasons. One that comes to my mind is different
> input types carrying the same information.
> 
> String(String original)
> String(char[] value)
> String(byte[] bytes, String charsetName)
> etc.
> 
> comes to mind.
> 
> Of course those are also tightly related.
> 
> Arne


In practise, I often find myself doing something like this:

public class Demo
{
   private String  name;
   private int     number;
   private boolean prisoner;

   public Demo(String name, int answer, boolean prisoner)
   {
      this.name = name;
      this.number = number;
      this.isPrisoner = prisoner;
   }

   public Demo(String name)
   {
      super(name, 6, true);
   }
}

... and so on. 

IOW there's one constructor that can set all attributes that might be set 
externally and a number of others that accept fewer arguments and set 
appropriate defaults for the other attributes.


     
-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#36794

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-07-03 08:38 -0400
Message-ID<ojddsm$8df$1@gioia.aioe.org>
In reply to#36793
On 7/3/2017 6:59 AM, Martin Gregorie wrote:
> In practise, I often find myself doing something like this:
> 
> public class Demo
> {
>     private String  name;
>     private int     number;
>     private boolean prisoner;
> 
>     public Demo(String name, int answer, boolean prisoner)
>     {
>        this.name = name;
>        this.number = number;
>        this.isPrisoner = prisoner;
>     }
> 
>     public Demo(String name)
>     {
>        super(name, 6, true);
>     }
> }
> 
> ... and so on.
> 
> IOW there's one constructor that can set all attributes that might be set
> externally and a number of others that accept fewer arguments and set
> appropriate defaults for the other attributes.

Yes.

That is what I consider the missing default parameter issue.

In some languages:

      public Demo(String name, int answer, boolean prisoner)
      {
         this.name = name;
         this.number = number;
         this.isPrisoner = prisoner;
      }
      public Demo(String name)
      {
         super(name, 6, true);
      }

can be made as:

      // **** NOT JAVA ****
      public Demo(String name, int answer = 6, boolean prisoner = false)
      {
         this.name = name;
         this.number = number;
         this.isPrisoner = prisoner;
      }

Arne

[toc] | [prev] | [next] | [standalone]


#36796

FromAndreas Leitgeb <avl@logic.at>
Date2017-07-03 14:04 +0000
Message-ID<slrnolkjtk.o2q.avl@logic.at>
In reply to#36794
Arne Vajhøj <arne@vajhoej.dk> wrote:
> That is what I consider the missing default parameter issue.
>       // **** NOT JAVA ****
>       public Demo(String name, int answer = 6, boolean prisoner = false)

Imho this is one thing that Java actually got right, by *not* supporting.

In C++, default values can give a hard time when overiding methods with
defaults, as the default value depends on the static target object
type even for virtual methods.

[toc] | [prev] | [next] | [standalone]


#36797

FromJerry Stuckle <jstucklex@attglobal.net>
Date2017-07-03 10:39 -0400
Message-ID<ojdkmu$u09$1@jstuckle.eternal-september.org>
In reply to#36796
On 7/3/2017 10:04 AM, Andreas Leitgeb wrote:
> Arne Vajhøj <arne@vajhoej.dk> wrote:
>> That is what I consider the missing default parameter issue.
>>       // **** NOT JAVA ****
>>       public Demo(String name, int answer = 6, boolean prisoner = false)
> 
> Imho this is one thing that Java actually got right, by *not* supporting.
> 
> In C++, default values can give a hard time when overiding methods with
> defaults, as the default value depends on the static target object
> type even for virtual methods.
> 

And how so?  It shouldn't happen if your design is correct.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#36799

FromAndreas Leitgeb <avl@logic.at>
Date2017-07-03 16:28 +0000
Message-ID<slrnolkscr.o2q.avl@logic.at>
In reply to#36797
Jerry Stuckle <jstucklex@attglobal.net> wrote:
> On 7/3/2017 10:04 AM, Andreas Leitgeb wrote:
>> Arne Vajhøj <arne@vajhoej.dk> wrote:
>>> That is what I consider the missing default parameter issue.
>>>       // **** NOT JAVA ****
>>>       public Demo(String name, int answer = 6, boolean prisoner = false)
>> Imho this is one thing that Java actually got right, by *not* supporting.
>> In C++, default values can give a hard time when overiding methods with
>> defaults, as the default value depends on the static target object
>> type even for virtual methods.
> And how so?  It shouldn't happen if your design is correct.

Not sure what you mean...  Do you mean that one just shouldn't have
a subclass giving different defaults for the override of a virtual 
method?

Maybe.  About 20 years ago, I had a case, where I thought it made
sense, and iirc I then rewrote it to two separate methods and let
the (ditto virtual) "no-args" overload call the "arg-taking" one
with the subclass's overridden value - i.e. I ended up doing it like
one would do it in Java, anyway - and back then, I didn't yet program
Java.

I still casually use defaults in my C++ projects, but only for
non-virtual methods. And it's just syntactic sugar in those cases,
and later I usually regret it, when I need to consult the .hpp file
to see what really gets called - even within the same class.

[toc] | [prev] | [next] | [standalone]


#36801

FromJerry Stuckle <jstucklex@attglobal.net>
Date2017-07-03 16:52 -0400
Message-ID<ojeais$9ek$1@jstuckle.eternal-september.org>
In reply to#36799
On 7/3/2017 12:28 PM, Andreas Leitgeb wrote:
> Jerry Stuckle <jstucklex@attglobal.net> wrote:
>> On 7/3/2017 10:04 AM, Andreas Leitgeb wrote:
>>> Arne Vajhøj <arne@vajhoej.dk> wrote:
>>>> That is what I consider the missing default parameter issue.
>>>>       // **** NOT JAVA ****
>>>>       public Demo(String name, int answer = 6, boolean prisoner = false)
>>> Imho this is one thing that Java actually got right, by *not* supporting.
>>> In C++, default values can give a hard time when overiding methods with
>>> defaults, as the default value depends on the static target object
>>> type even for virtual methods.
>> And how so?  It shouldn't happen if your design is correct.
> 
> Not sure what you mean...  Do you mean that one just shouldn't have
> a subclass giving different defaults for the override of a virtual 
> method?
>

So you have a derived class with different defaults.  Why would that be
a problem?  They would be appropriate for the subclass.

> Maybe.  About 20 years ago, I had a case, where I thought it made
> sense, and iirc I then rewrote it to two separate methods and let
> the (ditto virtual) "no-args" overload call the "arg-taking" one
> with the subclass's overridden value - i.e. I ended up doing it like
> one would do it in Java, anyway - and back then, I didn't yet program
> Java.
> 
> I still casually use defaults in my C++ projects, but only for
> non-virtual methods. And it's just syntactic sugar in those cases,
> and later I usually regret it, when I need to consult the .hpp file
> to see what really gets called - even within the same class.
> 

I use them quite often, and have never had a problem like you describe
on any project I've been on.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#36802

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-07-03 22:04 -0400
Message-ID<ojet3n$ek2$1@gioia.aioe.org>
In reply to#36796
On 7/3/2017 10:04 AM, Andreas Leitgeb wrote:
> Arne Vajhøj <arne@vajhoej.dk> wrote:
>> That is what I consider the missing default parameter issue.
>>        // **** NOT JAVA ****
>>        public Demo(String name, int answer = 6, boolean prisoner = false)
> 
> Imho this is one thing that Java actually got right, by *not* supporting.
> 
> In C++, default values can give a hard time when overiding methods with
> defaults, as the default value depends on the static target object
> type even for virtual methods.

Yes.

But is that really a problem?

It seems like what one would expect.

And changing the default value when overriding should be
documented rather well as it is a somewhat suspicious
thing to do.

I hope that a static code analysis tool create warnings
about doing something like that.

Arne



[toc] | [prev] | [next] | [standalone]


Page 1 of 4  [1] 2 3 4  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web