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

B

bilsch

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

Volker Borchert

bilsch 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?
public class CalcFrame1 extends JFrame{

public void CalcFrame1() {

This is not a constructor, but a method named like one.
 
E

Eric Sosman

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".
 
J

Jim Janney

bilsch 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?

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() {
 
B

bilsch

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.
 
B

bilsch

bilsch 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?

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?
 
E

Eric Sosman

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.
 
V

Volker Borchert

bilsch said:
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.
 
J

Jim Janney

bilsch said:
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.
 
S

Stefan Ram

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«.
 
S

Stefan Ram

Jim Janney said:
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.
 
J

Jim Janney

Jim Janney said:
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.
 
S

Stefan Ram

Jim Janney said:
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.
 
J

Jim Janney

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.
 
V

Volker Borchert

Jim said:
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.
 
R

Robert Klemme

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
 
S

Stefan Ram

Robert Klemme said:
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()«.
 
R

Roedy Green

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
..
 
S

Stefan Ram

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«.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top