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


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

It doesn't like 'super' where ever I put it.

Started bybilsch <bilsch01@gmail.com>
First post2012-06-10 06:27 -0700
Last post2012-06-11 16:41 -0700
Articles 15 on this page of 35 — 14 participants

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


Contents

  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 2 of 2 — ← Prev page 1 [2]


#15171

FromJim Janney <jjanney@shell.xmission.com>
Date2012-06-10 08:02 -0600
Message-ID<ydny5nvgt9d.fsf@shell.xmission.com>
In reply to#15168
bilsch <bilsch01@gmail.com> writes:

> 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)

Try changing

    public void CalcFrame1() {

to

    public CalcFrame1() {

-- 
Jim Janney

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


#15173

Frombilsch <bilsch01@gmail.com>
Date2012-06-10 07:20 -0700
Message-ID<jr2aet$pv7$1@dont-email.me>
In reply to#15171
On 6/10/2012 7:02 AM, Jim Janney wrote:
> bilsch<bilsch01@gmail.com>  writes:
>
>> 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)
>
> Try changing
>
>      public void CalcFrame1() {
>
> to
>
>      public CalcFrame1() {
>
OK.  Thanks.  Can you tell me why that makes a difference?

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


#15174

FromLeif Roar Moldskred <leifm@dimnakorr.com>
Date2012-06-10 09:49 -0500
Message-ID<xq2dndyU7NCUKUnSnZ2dnUVZ8gOdnZ2d@giganews.com>
In reply to#15173
bilsch <bilsch01@gmail.com> wrote:
>
> OK.  Thanks.  Can you tell me why that makes a difference?
> 

"void" is a special return type which denotes "returns nothing." It's
a rule of Java syntax that the declaration of a constructor _must not_
have a return type, while the declaration of a method which _must_
have one.

-- 
Leif Roar Moldskred

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


#15193

Frombilsch <bilsch01@gmail.com>
Date2012-06-10 13:15 -0700
Message-ID<jr2v9a$32l$1@dont-email.me>
In reply to#15174
On 6/10/2012 7:49 AM, Leif Roar Moldskred wrote:
> bilsch<bilsch01@gmail.com>  wrote:
>>
>> OK.  Thanks.  Can you tell me why that makes a difference?
>>
>
> "void" is a special return type which denotes "returns nothing." It's
> a rule of Java syntax that the declaration of a constructor _must not_
> have a return type, while the declaration of a method which _must_
> have one.
>
Thanks.

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


#15177

FromJim Janney <jjanney@shell.xmission.com>
Date2012-06-10 10:26 -0600
Message-ID<ydntxyjgmkb.fsf@shell.xmission.com>
In reply to#15173
bilsch <bilsch01@gmail.com> writes:

> On 6/10/2012 7:02 AM, Jim Janney wrote:
>> bilsch<bilsch01@gmail.com>  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.

-- 
Jim Janney

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


#15180

FromJim Janney <jjanney@shell.xmission.com>
Date2012-06-10 11:24 -0600
Message-ID<ydnpq97gjwk.fsf@shell.xmission.com>
In reply to#15177
Jim Janney <jjanney@shell.xmission.com> writes:

> bilsch <bilsch01@gmail.com> writes:
>
>> On 6/10/2012 7:02 AM, Jim Janney wrote:
>>> bilsch<bilsch01@gmail.com>  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

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


#15182

FromJim Janney <jjanney@shell.xmission.com>
Date2012-06-10 11:38 -0600
Message-ID<ydnlijvgj8g.fsf@shell.xmission.com>
In reply to#15177
ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Jim Janney <jjanney@shell.xmission.com> writes:
>>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();
>
>   When you find that you happen to write certain code parts
>   repeatedly, you can write a function in C:
>
> struct alpha * new_alpha()
> { struct alpha * const alpha = malloc( sizeof *alpha );
>   if( alpha )alpha->init(); return alpha; }
>
>   . This also is more safe, because it uses »alpha« only if
>   it is nonzero.

Yes, and this is a good idea.  I was trying to explain the distinction
between allocating memory for an object and actually initializing it,
and I couldn't think of a way to do that without referring to a language
that lets you separate them.

-- 
Jim Janney

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


#15187

Fromv_borchert@despammed.com (Volker Borchert)
Date2012-06-10 18:17 +0000
Message-ID<jr2ob2$5k5$1@Gaia.teknon.de>
In reply to#15182
Jim Janney wrote:
> Yes, and this is a good idea.  I was trying to explain the distinction
> between allocating memory for an object and actually initializing it,
> and I couldn't think of a way to do that without referring to a language
> that lets you separate them.

A look at the bytecode shows that Java does separate them, internally.

-- 

"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]


#15191

FromLew <noone@lewscanon.com>
Date2012-06-10 12:26 -0700
Message-ID<jr2sdb$fmc$1@news.albasani.net>
In reply to#15187
Volker Borchert wrote:
> Jim Janney wrote:
>> Yes, and this is a good idea.  I was trying to explain the distinction
>> between allocating memory for an object and actually initializing it,
>> and I couldn't think of a way to do that without referring to a language
>> that lets you separate them.
>
> A look at the bytecode shows that Java does separate them, internally.

No, it shows that the JVM separates them. Java does not.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

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


#15194

Frombilsch <bilsch01@gmail.com>
Date2012-06-10 13:17 -0700
Message-ID<jr2vca$32l$2@dont-email.me>
In reply to#15177
On 6/10/2012 9:26 AM, Jim Janney wrote:
> bilsch<bilsch01@gmail.com>  writes:
>
>> On 6/10/2012 7:02 AM, Jim Janney wrote:
>>> bilsch<bilsch01@gmail.com>   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.
>
Thanks for  the info.

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


#15186

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-06-10 11:54 -0700
Message-ID<89r9t7tk5mvtej1f1c8t9hkt0gsrfc7lcm@4ax.com>
In reply to#15168
On Sun, 10 Jun 2012 06:27:41 -0700, bilsch <bilsch01@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>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?
 see http://mindprod.com/jgloss/super.html
http://mindprod.com/jgloss/contsructor.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
Controlling complexity is the essence of computer programming.
~ Brian W. Kernighan 1942-01-01
.

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


#15195

Frombilsch <bilsch01@gmail.com>
Date2012-06-10 13:24 -0700
Message-ID<jr2vp1$6p4$1@dont-email.me>
In reply to#15186
On 6/10/2012 11:54 AM, Roedy Green wrote:
> On Sun, 10 Jun 2012 06:27:41 -0700, bilsch<bilsch01@gmail.com>  wrote,
> quoted or indirectly quoted someone who said :
>
>> 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?
>   see http://mindprod.com/jgloss/super.html
> http://mindprod.com/jgloss/contsructor.html

  Thanks for the link.

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


#15197

FromLew <noone@lewscanon.com>
Date2012-06-10 13:56 -0700
Message-ID<jr31lk$rrp$1@news.albasani.net>
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?
>
> 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() {

Everyone else has told you of the difference a 'void' makes.

You asked where this is taught so you can learn it. The Java tutorial is the 
canonical starting place.

<http://docs.oracle.com/javase/tutorial/>
particularly
<http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html>

>    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);

Don't 'setVisible()' in the constructor. Constructors, as the name implies, 
are for construction of an object, but not its operation.

Let the object finish building itself completely before anything else can get 
to it.

>  }
>  private void setLookAndFeel(){
>    try{
>      UIManager.setLookAndFeel(
>      "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
>    }
>    catch(Exception exc){

Don't catch just 'Exception' here, but the exact exceptions you are required 
to catch.

>      // ignore error

And *never* ignore errors!

>    }
> }
>
>  public static void main(String[] args){
>    CalcFrame1 ClFr1 = new CalcFrame1();

Oops. You didn't start this on the EDT (Event Dispatch Thread). (And you don't 
let the object finish construction before things can get to it.)

The Swing tutorial (ibid.) explains this a little bit.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

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


#15221

Frombilsch <bilsch01@gmail.com>
Date2012-06-11 20:55 -0700
Message-ID<jr6eke$2k9$1@dont-email.me>
In reply to#15197
On 6/10/2012 1:56 PM, Lew 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?
>>
>> 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() {
>
> Everyone else has told you of the difference a 'void' makes.
>
> You asked where this is taught so you can learn it. The Java tutorial is
> the canonical starting place.
>
> <http://docs.oracle.com/javase/tutorial/>
> particularly
> <http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html>
>
>> 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);
>
> Don't 'setVisible()' in the constructor. Constructors, as the name
> implies, are for construction of an object, but not its operation.
>
> Let the object finish building itself completely before anything else
> can get to it.
>
>> }
>> private void setLookAndFeel(){
>> try{
>> UIManager.setLookAndFeel(
>> "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
>> }
>> catch(Exception exc){
>
> Don't catch just 'Exception' here, but the exact exceptions you are
> required to catch.
>
>> // ignore error
>
> And *never* ignore errors!
>
>> }
>> }
>>
>> public static void main(String[] args){
>> CalcFrame1 ClFr1 = new CalcFrame1();
>
> Oops. You didn't start this on the EDT (Event Dispatch Thread). (And you
> don't let the object finish construction before things can get to it.)
>
> The Swing tutorial (ibid.) explains this a little bit.
>
Thanks for the specific link.  I have been reading the tutorials.

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


#15213

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-06-11 16:41 -0700
Message-ID<wmvBr.3404$l_1.2055@newsfe21.iad>
In reply to#15168
On 6/10/12 6: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() {
> 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)
This is actually a runtime error, a problem with the  .class file.  Not 
sure how that happened, unless you have a faulty compiler.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web