Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: How to assign an object to another in it method? Date: Mon, 3 Jul 2017 17:28:22 -0000 (UTC) Organization: A noiseless patient Spider Lines: 73 Message-ID: References: <46334961-cc18-491f-828c-32e7401c7832@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 3 Jul 2017 17:28:22 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="c38faa1fa99f0be03b23f52420ce2346"; logging-data="27449"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6Im23cS2b7SQZLhH/liYmoUHsm6wyZTY=" User-Agent: Pan/0.141 (Tarzan's Death; 168b179 git.gnome.org/pan2) Cancel-Lock: sha1:Nyn4s43avdoJF72ypFHCWB+rRSk= Xref: csiph.com comp.lang.java.programmer:36800 On Mon, 03 Jul 2017 11:34:39 -0400, Eric Sosman wrote: > On 7/3/2017 8:37 AM, Martin Gregorie wrote: >> On Mon, 03 Jul 2017 10:59:12 +0000, Martin Gregorie wrote: >> >> Hadn't checked/compiled the previous example, which of course should >> be: >> >> 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; > > For some reason, NetBeans warns about this line ... :( > > (Don't feel too bad, Martin: Some Days Are Just Like That, > and I've had plenty of them.) Interesting: it compiled with no errors, using javac 1.8.0_91, so I extended it a bit to this so it actually did something: public class Demo { private String name; private int number; private boolean prisoner; public Demo(String name, int number, boolean prisoner) { this.name = name; this.number = number; this.prisoner = prisoner; } public Demo(String name) { this(name, 6, true); } public void dialogue() { System.out.println("You are now " + (prisoner ? "a prisoner" : "free to leave")); System.out.println("I, " + name + ", am a free man!"); System.out.println("You are number " + number); } public static void main(String[] args) { Demo d = new Demo("Patrick McGoohan"); d.dialogue(); } } ... and promptly found the mistake in the first constructor (2nd argument had 'answer' as it sname, causing this.number to be assigned to itself). I assume that this is what NetBeans objected to? Anyway, this is fixed in the version shown above and runs as expected. BTW, I don't use IDEs at all, preferring to work with a bog-standard text editor and a couple of command-line consoles. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |