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

Discussion in 'Java' started by bilsch, Jun 10, 2012.

  1. bilsch

    bilsch Guest

    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)
    bilsch, Jun 10, 2012
    #1
    1. Advertising

  2. 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 <>
    "I'm a mechanic, not a doctor." Volker Borchert <>
    Volker Borchert, Jun 10, 2012
    #2
    1. Advertising

  3. bilsch

    Eric Sosman Guest

    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
    d
    Eric Sosman, Jun 10, 2012
    #3
  4. bilsch

    Jim Janney Guest

    bilsch <> 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
    Jim Janney, Jun 10, 2012
    #4
  5. bilsch

    bilsch Guest

    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.
    bilsch, Jun 10, 2012
    #5
  6. bilsch

    bilsch Guest

    On 6/10/2012 7:02 AM, Jim Janney wrote:
    > bilsch<> 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?
    bilsch, Jun 10, 2012
    #6
  7. bilsch

    Eric Sosman Guest

    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
    d
    Eric Sosman, Jun 10, 2012
    #7
  8. 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 <>
    "I'm a mechanic, not a doctor." Volker Borchert <>
    Volker Borchert, Jun 10, 2012
    #8
  9. bilsch

    Jim Janney Guest

    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.

    --
    Jim Janney
    Jim Janney, Jun 10, 2012
    #9
  10. bilsch

    Stefan Ram Guest

    (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«.
    Stefan Ram, Jun 10, 2012
    #10
  11. bilsch

    Stefan Ram Guest

    Jim Janney <> 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.
    Stefan Ram, Jun 10, 2012
    #11
  12. bilsch

    Jim Janney Guest

    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
    Jim Janney, Jun 10, 2012
    #12
  13. bilsch

    Stefan Ram Guest

    Jim Janney <> writes:
    >the new operator is responsible for creating the object.


    The new keyword is part of the instance creation expression,
    but it is not an operator according to the JLS.
    Stefan Ram, Jun 10, 2012
    #13
  14. bilsch

    Jim Janney Guest

    -berlin.de (Stefan Ram) writes:

    > Jim Janney <> 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
    Jim Janney, Jun 10, 2012
    #14
  15. 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 <>
    "I'm a mechanic, not a doctor." Volker Borchert <>
    Volker Borchert, Jun 10, 2012
    #15
  16. On 10.06.2012 19:10, Stefan Ram wrote:
    > (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/
    Robert Klemme, Jun 10, 2012
    #16
  17. bilsch

    Stefan Ram Guest

    Robert Klemme <> writes:
    >On 10.06.2012 19:10, Stefan Ram wrote:
    >> (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()«.
    Stefan Ram, Jun 10, 2012
    #17
  18. bilsch

    Roedy Green Guest

    On Sun, 10 Jun 2012 06:27:41 -0700, bilsch <> 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
    ..
    Roedy Green, Jun 10, 2012
    #18
  19. bilsch

    Stefan Ram Guest

    (Volker Borchert) writes:
    >A look at the bytecode shows that Java does separate them, internally.


    A look at the bytecode might reveal internals of a specific
    implementation, but not of the Java language, which is
    defined by the JLS and has no further »inside«.
    Stefan Ram, Jun 10, 2012
    #19
  20. bilsch

    Lew Guest

    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
    Lew, Jun 10, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,738
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    708
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    466
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. Jason
    Replies:
    0
    Views:
    179
    Jason
    Jul 6, 2004
Loading...

Share This Page