Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #15168 > unrolled thread
| Started by | bilsch <bilsch01@gmail.com> |
|---|---|
| First post | 2012-06-10 06:27 -0700 |
| Last post | 2012-06-11 16:41 -0700 |
| Articles | 20 on this page of 35 — 14 participants |
Back to article view | Back to comp.lang.java.programmer
It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 06:27 -0700
Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 13:49 +0000
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 07:08 -0700
Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-10 11:34 -0400
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 12:58 -0700
Re: It doesn't like 'super' where ever I put it. markspace <-@.> - 2012-06-10 13:43 -0700
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-11 21:01 -0700
Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 15:44 +0000
Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-10 20:39 +0200
Re: It doesn't like 'super' where ever I put it. "Gavino" <invalid@invalid.invalid> - 2012-06-11 20:13 +0200
Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-11 22:51 +0200
Re: It doesn't like 'super' where ever I put it. "Gavino" <invalid@invalid.invalid> - 2012-06-12 00:45 +0200
Re: It doesn't like 'super' where ever I put it. Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-11 22:53 +0000
Re: It doesn't like 'super' where ever I put it. Lew <lewbloch@gmail.com> - 2012-06-11 16:42 -0700
Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-11 19:53 -0400
Re: It doesn't like 'super' where ever I put it. Lew <lewbloch@gmail.com> - 2012-06-11 17:38 -0700
Re: It doesn't like 'super' where ever I put it. "John B. Matthews" <nospam@nospam.invalid> - 2012-06-11 23:14 -0400
Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-12 18:21 +0200
Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-11 22:50 +0200
Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-10 09:59 -0400
Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 08:02 -0600
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 07:20 -0700
Re: It doesn't like 'super' where ever I put it. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-06-10 09:49 -0500
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:15 -0700
Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 10:26 -0600
Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 11:24 -0600
Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 11:38 -0600
Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 18:17 +0000
Re: It doesn't like 'super' where ever I put it. Lew <noone@lewscanon.com> - 2012-06-10 12:26 -0700
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:17 -0700
Re: It doesn't like 'super' where ever I put it. Roedy Green <see_website@mindprod.com.invalid> - 2012-06-10 11:54 -0700
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:24 -0700
Re: It doesn't like 'super' where ever I put it. Lew <noone@lewscanon.com> - 2012-06-10 13:56 -0700
Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-11 20:55 -0700
Re: It doesn't like 'super' where ever I put it. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-11 16:41 -0700
Page 1 of 2 [1] 2 Next page →
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-10 06:27 -0700 |
| Subject | It doesn't like 'super' where ever I put it. |
| Message-ID | <jr27ce$7uo$1@dont-email.me> |
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?
The error output is listed below the program
TIA Bill S.
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class CalcFrame1 extends JFrame{
public void CalcFrame1() {
super("CalcFrame1");
//setTitle("CalcFrame1");
FlowLayout flo = new FlowLayout();
setLayout(flo);
setLookAndFeel();
//setSize(600,600);
JButton shf = new JButton("shft");
JButton chs = new JButton("chs");
add (shf);
add (chs);
pack();
setVisible(true);
}
private void setLookAndFeel(){
try{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception exc){
// ignore error
}
}
public static void main(String[] args){
CalcFrame1 ClFr1 = new CalcFrame1();
}
}
ERROR OUTPUT:
java.lang.VerifyError: Constructor must call super() or this() before
return in method CalcFrame1.<init>()V at offset 0
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
[toc] | [next] | [standalone]
| From | v_borchert@despammed.com (Volker Borchert) |
|---|---|
| Date | 2012-06-10 13:49 +0000 |
| Message-ID | <jr28la$1dp$1@Gaia.teknon.de> |
| In reply to | #15168 |
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 a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_borchert@despammed.com>
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-10 07:08 -0700 |
| Message-ID | <jr29ot$ltb$1@dont-email.me> |
| In reply to | #15169 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-06-10 11:34 -0400 |
| Message-ID | <jr2epp$kja$1@dont-email.me> |
| In reply to | #15172 |
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.<init>" as
a sort of stand-in for the name -- but you quite clearly
can't use "<init>" 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
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-10 12:58 -0700 |
| Message-ID | <jr2u9a$s3t$1@dont-email.me> |
| In reply to | #15175 |
On 6/10/2012 8:34 AM, Eric Sosman wrote:
> 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.<init>" as
> a sort of stand-in for the name -- but you quite clearly
> can't use "<init>" 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.
>
Thanks for the explanation. I have some Java books. They are kind of
vague on the subject.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-06-10 13:43 -0700 |
| Message-ID | <jr30sm$e2h$1@dont-email.me> |
| In reply to | #15192 |
On 6/10/2012 12:58 PM, bilsch wrote: > Thanks for the explanation. I have some Java books. They are kind of > vague on the subject. I like to keep in mind lists of books that aren't good Java books, as well as those that are good, just to be able to recommend them (or not) when questions are asked on this list. Care to let us know which books you find wanting? FYI, I own and recommend Learning Java, published by O'Reilly, third edition. I don't recall exactly where I learned that constructors have no return type keyword, but I'm sure it's in there. I also recommend the Java tutorial on Oracle's website. It's not in-depth, but it often is useful to answer specific questions. <http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html> "Constructor declarations look like method declarations—except that they use the name of the class and have no return type"
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-11 21:01 -0700 |
| Message-ID | <jr6eu8$2k9$2@dont-email.me> |
| In reply to | #15196 |
On 6/10/2012 1:43 PM, markspace wrote: > On 6/10/2012 12:58 PM, bilsch wrote: > > Thanks for the explanation. I have some Java books. They are kind of >> vague on the subject. > > > I like to keep in mind lists of books that aren't good Java books, as > well as those that are good, just to be able to recommend them (or not) > when questions are asked on this list. Care to let us know which books > you find wanting? > > FYI, I own and recommend Learning Java, published by O'Reilly, third > edition. I don't recall exactly where I learned that constructors have > no return type keyword, but I'm sure it's in there. > > I also recommend the Java tutorial on Oracle's website. It's not > in-depth, but it often is useful to answer specific questions. > > <http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html> > > "Constructor declarations look like method declarations—except that they > use the name of the class and have no return type" > Thanks for the link. I've been reading the tutorials.
[toc] | [prev] | [next] | [standalone]
| From | v_borchert@despammed.com (Volker Borchert) |
|---|---|
| Date | 2012-06-10 15:44 +0000 |
| Message-ID | <jr2fc4$3e1$1@Gaia.teknon.de> |
| In reply to | #15172 |
bilsch wrote: > Please tell me what I need to add to the program to create a > proper constructor. Others already have done so. I'd like to augment this with a) Go back to your text book and reread the chapter on constructors. If the definition and explanation there is too vague, get a better text book. If you don't have any text book yet, get one. b) Check out the online tutorials on wherever java.sun.com redirects. c) Look into the sources of JFrame to see how its constructors are defined. Not all of the JRE is perfect in design and style, in fact there are some flaws they have decided to let live on for compatibility's sake, but as examples for the basics, it'll do. -- "I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed> "I'm a mechanic, not a doctor." Volker Borchert <v_borchert@despammed.com>
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2012-06-10 20:39 +0200 |
| Message-ID | <a3k7vfF3k1U1@mid.individual.net> |
| In reply to | #15169 |
On 10.06.2012 19:10, Stefan Ram wrote: > v_borchert@despammed.com (Volker Borchert) writes: >> This is not a constructor, but a method named like one. > > I'm not sure, whether this explains the error message given: > > |Constructor must call super() or this() before > > , because when there is no explicit constructor, the compiler > will generate one, and this generated constructor /should/ > call »super() or this() before«. The compiler will only generate a default constructor (i.e. without arguments) if there is no other constructor. That generated default constructor cannot invoke any other superclass constructor than the default constructor (where should it take arguments from?). I'm too lazy to lookup in JLS right now (Ireland - Croatia starts in a few minutes) but I am sure you'll find it explained in the JLS. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | "Gavino" <invalid@invalid.invalid> |
|---|---|
| Date | 2012-06-11 20:13 +0200 |
| Message-ID | <a3mqqdFq23U1@mid.individual.net> |
| In reply to | #15183 |
"Stefan Ram" <ram@zedat.fu-berlin.de> wrote in message news:constructor-20120610205100@ram.dialup.fu-berlin.de... > Robert Klemme <shortcutter@googlemail.com> writes: > >On 10.06.2012 19:10, Stefan Ram wrote: > >> v_borchert@despammed.com (Volker Borchert) writes: > >>> This is not a constructor, but a method named like one. > >>I'm not sure, whether this explains the error message given: > >>|Constructor must call super() or this() before > >>, because when there is no explicit constructor, the compiler > >>will generate one, and this generated constructor /should/ > >>call »super() or this() before«. > >The compiler will only generate a default constructor (i.e. without > >arguments) if there is no other constructor. > > There was no (other) constructor in the code of the OP, > so a default constructor must have been generated, which > will include a correct call of »super()«. It can't because the superclass (JFrame) does not have a constructor without arguments.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2012-06-11 22:51 +0200 |
| Message-ID | <a3n43dF3fbU2@mid.individual.net> |
| In reply to | #15206 |
On 11.06.2012 20:13, Gavino wrote: > "Stefan Ram"<ram@zedat.fu-berlin.de> wrote in message > news:constructor-20120610205100@ram.dialup.fu-berlin.de... >> Robert Klemme<shortcutter@googlemail.com> writes: >>> On 10.06.2012 19:10, Stefan Ram wrote: >>>> v_borchert@despammed.com (Volker Borchert) writes: >>>>> This is not a constructor, but a method named like one. >>>> I'm not sure, whether this explains the error message given: >>>> |Constructor must call super() or this() before >>>> , because when there is no explicit constructor, the compiler >>>> will generate one, and this generated constructor /should/ >>>> call »super() or this() before«. >>> The compiler will only generate a default constructor (i.e. without >>> arguments) if there is no other constructor. >> >> There was no (other) constructor in the code of the OP, >> so a default constructor must have been generated, which >> will include a correct call of »super()«. > > It can't because the superclass (JFrame) does not have a constructor without > arguments. Also wrong. http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#JFrame() robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | "Gavino" <invalid@invalid.invalid> |
|---|---|
| Date | 2012-06-12 00:45 +0200 |
| Message-ID | <a3naocFkvmU1@mid.individual.net> |
| In reply to | #15208 |
"Robert Klemme" <shortcutter@googlemail.com> wrote in message news:a3n43dF3fbU2@mid.individual.net... On 11.06.2012 20:13, Gavino wrote: >> It can't because the superclass (JFrame) does not have a constructor without >> arguments. > Also wrong. > http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#JFrame() My apologies. I don't know where I got that erroneous idea from, but somehow I've been carrying it around in my head for the last 12 years. I should have checked the JavaDocs instead of relying on badly remembered information. Now I'm equally confused about the wording of the error message.
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2012-06-11 22:53 +0000 |
| Message-ID | <jr5su2$rv7$1@localhost.localdomain> |
| In reply to | #15209 |
On Tue, 12 Jun 2012 00:45:28 +0200, Gavino wrote:
> "Robert Klemme" <shortcutter@googlemail.com> wrote in message
> news:a3n43dF3fbU2@mid.individual.net...
> On 11.06.2012 20:13, Gavino wrote:
>>> It can't because the superclass (JFrame) does not have a constructor
>>> without arguments.
>
>> Also wrong.
>
>> http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#JFrame
()
>
> My apologies.
> I don't know where I got that erroneous idea from, but somehow I've been
> carrying it around in my head for the last 12 years.
> I should have checked the JavaDocs instead of relying on badly
> remembered information.
>
> Now I'm equally confused about the wording of the error message.
As Stefan said, I believe its objecting to the call to
super("CalcFrame1") in the class erroneously declared as
"public void CalcFrame1()".
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-06-11 16:42 -0700 |
| Message-ID | <52179c3c-c8d0-4ff6-abce-78b8fa4d154b@googlegroups.com> |
| In reply to | #15211 |
Martin Gregorie wrote:
> Gavino wrote:
>> Robert Klemme wrote ...
>> Gavino wrote:
>>>> It can't because the superclass (JFrame) does not have a constructor
>>>> without arguments.
>>
>>> Also wrong.
>>
>>> <http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#JFrame()>
>>
>> My apologies.
>> I don't know where I got that erroneous idea from, but somehow I've been
>> carrying it around in my head for the last 12 years.
>> I should have checked the JavaDocs instead of relying on badly
>> remembered information.
>>
>> Now I'm equally confused about the wording of the error message.
>
> As Stefan said, I believe its objecting to the call to
> super("CalcFrame1") in the class erroneously declared as
> "public void CalcFrame1()".
Here's the chain:
The rule for 'super()' constructor calls is that they must appear only as the first line in a constructor. The OP put it in a different place, inside a method, violating the rule stated in the error message:
"Constructor must call super() or this() before return in method CalcFrame1.<init>()V at offset 0"
That's not where the OP's code called 'super()'. Ergo, it violated that rule.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-06-11 19:53 -0400 |
| Message-ID | <jr60e0$25m$1@dont-email.me> |
| In reply to | #15209 |
On 6/11/2012 6:45 PM, Gavino wrote:
> "Robert Klemme" <shortcutter@googlemail.com> wrote in message
> news:a3n43dF3fbU2@mid.individual.net...
> On 11.06.2012 20:13, Gavino wrote:
>>> It can't because the superclass (JFrame) does not have a constructor without
>>> arguments.
>
>> Also wrong.
>
>> http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#JFrame()
>
> My apologies.
> I don't know where I got that erroneous idea from, but somehow I've been carrying it
> around in my head for the last 12 years.
> I should have checked the JavaDocs instead of relying on badly remembered information.
>
> Now I'm equally confused about the wording of the error message.
You are not the only confused person. It might just be clumsy
wording:
Constructor must call super() or this() before return in
method CalcFrame1.<init>()V [...]
certainly seems to be complaining about a constructor: That's the first
word of the message, and <init> is what one usually sees when a message
tries to mention the "name" of a (nameless) constructor.
The message *seems* to be saying: "The constructor fails to call
super() or this() when it ought to." Instead, what I think it means
is "The super() or this() call should be in a constructor, not in a
method (where it makes no sense)."
One must, however, cut the compiler a certain amount of slack.
There's a built-in ambiguity about diagnosing "the" error in erroneous
source. All the compiler knows for sure is that the source is wrong;
to explain "how" it is wrong, the compiler must guess at what it tried
(and failed) to express. That is, there are many possible compilable
programs at smaller and larger "edit distances" from the incorrect code
at hand, and the compiler has no way to know which was intended. To
describe "the error," the compiler must choose one (or a small family)
of those nearby candidates and describe how the offered code differs.
But it's all guesswork -- even a compilable program may not be what was
intended (that's why we debug...).
The practical lesson is not to take error messages too literally.
Very often the message will highlight the difficulty well enough to
prompt you to a facepalm and a fix. But when the message seems at an
inexplicable tangent to what you think is going on, the best approach
may be to reinterpret "Operand of binary % must be a duodecimal dingle"
as "Something's wrong," and read no more into it than that.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-06-11 17:38 -0700 |
| Message-ID | <5d496e1f-37e1-43d2-a54c-47dacd3c3fec@googlegroups.com> |
| In reply to | #15215 |
Stefan Ram wrote:
> Eric Sosman writes:
> >Constructor must call super() or this() before return in
> >method CalcFrame1.<init>()V [...] (...)
> >at hand, and the compiler has no way to know which was intended.
>
> But the above is not a compiler message, but a run-time message.
Perhaps there was a transcription error in the OP's message when they put
together their otherwise quite serviceable SSCCE.
> For a similar situation, I get a compiler message:
>
> |Main.java:31: error: call to super must be first statement in constructor
> |public void test(){ super(); }
>
> . (This message does not actually claim that »test« was a
> constructor, although one might read it this way.)
Has anyone tried compiling the OP's first example? (I haven't, and won't.)
Perhaps there's a further anomaly in there.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | "John B. Matthews" <nospam@nospam.invalid> |
|---|---|
| Date | 2012-06-11 23:14 -0400 |
| Message-ID | <nospam-2C6A71.23140711062012@news.aioe.org> |
| In reply to | #15217 |
In article <5d496e1f-37e1-43d2-a54c-47dacd3c3fec@googlegroups.com>,
Lew <lewbloch@gmail.com> wrote:
> Stefan Ram wrote:
> > Eric Sosman writes:
> > >Constructor must call super() or this() before return in
> > >method CalcFrame1.<init>()V [...] (...)
> > >at hand, and the compiler has no way to know which was intended.
> >
> > But the above is not a compiler message, but a run-time message.
>
> Perhaps there was a transcription error in the OP's message when they put
> together their otherwise quite serviceable SSCCE.
>
> > For a similar situation, I get a compiler message:
> >
> > |Main.java:31: error: call to super must be first statement in constructor
> > |public void test(){ super(); }
> >
> > . (This message does not actually claim that »test« was a
> > constructor, although one might read it this way.)
>
> Has anyone tried compiling the OP's first example? (I haven't, and
> won't.) Perhaps there's a further anomaly in there.
bilsch: Summarizing several insightful answers, "Constructor
declarations look like method declarations—except that they use the
name of the class and have no return type [1]."
In addition to reviewing "Initial Threads" [2], also critically examine
your decision to extend JFrame. It may be more convenient to add a
JPanel containing buttons, as suggested in KeyPadPanel [3].
SSCCE:
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
public class CalcFrame extends JFrame {
public /* void */ CalcFrame() {
super("CalcFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new JButton("Button"));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CalcFrame cf = new CalcFrame();
}
});
}
}
[1] <http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html>
[2] <http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html>
[3] <https://sites.google.com/site/drjohnbmatthews/keypad-panel>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2012-06-12 18:21 +0200 |
| Message-ID | <a3p8l9Fbm1U1@mid.individual.net> |
| In reply to | #15217 |
On 12.06.2012 02:38, Lew wrote:
> Stefan Ram wrote:
>> Eric Sosman writes:
>>> Constructor must call super() or this() before return in
>>> method CalcFrame1.<init>()V [...] (...)
>>> at hand, and the compiler has no way to know which was intended.
>>
>> But the above is not a compiler message, but a run-time message.
>
> Perhaps there was a transcription error in the OP's message when they put
> together their otherwise quite serviceable SSCCE.
I rather think the confusion now reached a degree where people loose
sight of compile time and runtime. :-)
>> For a similar situation, I get a compiler message:
>>
>> |Main.java:31: error: call to super must be first statement in constructor
>> |public void test(){ super(); }
>>
>> . (This message does not actually claim that »test« was a
>> constructor, although one might read it this way.)
>
> Has anyone tried compiling the OP's first example? (I haven't, and won't.)
> Perhaps there's a further anomaly in there.
Yes, with Eclipse. See my posting from yesterday for Eclipse _compiler_
(and not runtime!) error message. Repeated for convenience:
> Granted, the error message is probably not the most telling. Eclipse says
> Constructor call must be the first statement in a constructor CalcFrame1.java /c.l.j.p/src line 8 Java Problem
>
> Which is certainly clearer.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2012-06-11 22:50 +0200 |
| Message-ID | <a3n40bF3fbU1@mid.individual.net> |
| In reply to | #15183 |
On 10.06.2012 20:51, Stefan Ram wrote:
> Robert Klemme<shortcutter@googlemail.com> writes:
>> On 10.06.2012 19:10, Stefan Ram wrote:
>>> v_borchert@despammed.com (Volker Borchert) writes:
>>>> This is not a constructor, but a method named like one.
>>> I'm not sure, whether this explains the error message given:
>>> |Constructor must call super() or this() before
>>> , because when there is no explicit constructor, the compiler
>>> will generate one, and this generated constructor /should/
>>> call »super() or this() before«.
>> The compiler will only generate a default constructor (i.e. without
>> arguments) if there is no other constructor.
>
> There was no (other) constructor in the code of the OP,
> so a default constructor must have been generated, which
> will include a correct call of »super()«.
>
Only that this won't be done for non constructors which is what the
method was, as has been pointed out
public void CalcFrame1() { ... }
Note that super(...) is always a constructor call because method calls
read super.method(...).
Granted, the error message is probably not the most telling. Eclipse says
Constructor call must be the first statement in a constructor
CalcFrame1.java /c.l.j.p/src line 8 Java Problem
Which is certainly clearer.
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-06-10 09:59 -0400 |
| Message-ID | <jr298g$ijh$1@dont-email.me> |
| In reply to | #15168 |
On 6/10/2012 9:27 AM, 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?
>
> The error output is listed below the program
>
> TIA Bill S.
>
>
> PROGRAM:
>
> import javax.swing.*;
> import java.awt.*;
> import java.util.*;
>
> public class CalcFrame1 extends JFrame{
>
> public void CalcFrame1() {
If you mean this to be a constructor, lose the "void".
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.java.programmer
csiph-web