Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: It doesn't like 'super' where ever I put it. Date: Sun, 10 Jun 2012 11:24:11 -0600 Organization: slowly at first, then all at once Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="dZdavj/jUDynNQgDq5jkeA"; logging-data="32274"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Cr4/3vyBOFS0ED6ph2Gnj" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:tgbDgPPKGEXi/h/qAMHx162IK4I= sha1:WKwuVSzd4w1mRS+zjsHYPlK4FDs= Xref: csiph.com comp.lang.java.programmer:15180 Jim Janney writes: > bilsch writes: > >> On 6/10/2012 7:02 AM, Jim Janney wrote: >>> bilsch writes: >>>> BUILD SUCCESSFUL (total time: 2 seconds) >>> >>> Try changing >>> >>> public void CalcFrame1() { >>> >>> to >>> >>> public CalcFrame1() { >>> >> OK. Thanks. Can you tell me why that makes a difference? > > Constructors are fundamentally different from ordinary methods, so most > object-oriented languages use a different syntax for them. In Java, a > constructor definition looks like a method, but without an explicit > return type and using a name that matches the class name. In other > languages the syntax may be different -- for example, in Python the name > is always __init__ -- but the underlying idea is the same: constructors > are not ordinary methods and take a different syntax. > > Why are constructors different? Most classes have some code that you > want to run whenever a new object is created, and it would be tedious to > have to call it explicitly every time. I used to mimic OOP in C and you > had to do things like > > struct Foo* foo = (struct Foo*) malloc(sizeof(struct Foo)); > foo->init(); > > Forcing all object creation to go through a constructor makes the code > simpler and more reliable. And even when a class has a constructor that > appears to be empty, there's still some code that the compiler generates > automatically the make the new object a proper instance of its class. The point to remember is that a constructor does not create an object: the new operator is responsible for creating the object. Along the way it kindly calls the constructor, giving you an opportunity to do some extra setup work. Understand this and you'll know why a constructor may throw an exception but it can never return null. Wandering off the original subject, I still notice allegedly professional programmers writing code like this: Foo foo = null; try { foo = new Foo("my resource name"); // may throw exception process(foo); // may throw exception } finally { if (foo != null) { foo.close(); } } It isn't actually wrong, but it makes me wonder what else they don't understand. -- Jim Janney