Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: It doesn't like 'super' where ever I put it. Date: Sun, 10 Jun 2012 11:34:10 -0400 Organization: A noiseless patient Spider Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 10 Jun 2012 15:34:17 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="03ebLEozl+tXCe7JS60Feg"; logging-data="21098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WO/Qrt+73LZl7Lsj9oCvh" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 In-Reply-To: Cancel-Lock: sha1:EC+kxzIhUDLrKar7FFSzfDVsVPE= Xref: csiph.com comp.lang.java.programmer:15175 On 6/10/2012 10:08 AM, bilsch wrote: > On 6/10/2012 6:49 AM, Volker Borchert wrote: >> bilsch wrote: >>> Hello, below is my program stripped to bare bones. Java says 'super' >>> must be first statement in constructor. I've moved it everywhere still >>> no luck. The program was running yesterday and I can't figure what >>> could be wrong. Any suggestions? >> >>> public class CalcFrame1 extends JFrame{ >>> >>> public void CalcFrame1() { >> >> This is not a constructor, but a method named like one. >> > I'm not exactly clear about constructors. Definitions I've seen are > vague. Please tell me what I need to add to the program to create a > proper constructor. Thanks. Add nothing; *subtract* "void". A constructor is a special piece of code that initializes a brand-new object. It looks superficially like a method, and you can write the same kinds of Java statements in a constructor as in a method, but it is not a method at all. Among the differences: - You can call methods, but you cannot "call" constructors. Constructors run when a `new' operation is performed, to set up the new object. There are a few operations that sort of look like "calls" to constructors -- one constructor can invoke another constructor of the same class with a this(...) construct, or a constructor of its superclass with a super(...) -- but as you've seen you cannot just "call" a constructor the way you'd call toString(). - Methods have names, but constructors don't. If a constructor throws an exception and a stack trace gets printed, you'll see something like "ClassName." as a sort of stand-in for the name -- but you quite clearly can't use "" as the name of a method! - Constructors have no return type, not even `void'. A method *always* has a return type, even if it's `void'. I think you need a Java textbook, just like everyone else did when starting out. -- Eric Sosman esosman@ieee-dot-org.invalid