Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > It doesn't like 'super' where ever I put it.

Reply
Thread Tools

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

 
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
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.ja va:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(Launcher Helper.java:492)
at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:484)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
 
Reply With Quote
 
 
 
 
Volker Borchert
Guest
Posts: n/a
 
      06-10-2012
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 <(E-Mail Removed)>
"I'm a mechanic, not a doctor." Volker Borchert <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-10-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Jim Janney
Guest
Posts: n/a
 
      06-10-2012
bilsch <(E-Mail Removed)> 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.ja va:2442)
> at java.lang.Class.getMethod0(Class.java:2685)
> at java.lang.Class.getMethod(Class.java:1620)
> at sun.launcher.LauncherHelper.getMainMethod(Launcher Helper.java:492)
> at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.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
 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
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.
 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
On 6/10/2012 7:02 AM, Jim Janney wrote:
> bilsch<(E-Mail Removed)> 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.ja va:2442)
>> at java.lang.Class.getMethod0(Class.java:2685)
>> at java.lang.Class.getMethod(Class.java:1620)
>> at sun.launcher.LauncherHelper.getMainMethod(Launcher Helper.java:492)
>> at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.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?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-10-2012
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
(E-Mail Removed)d
 
Reply With Quote
 
Volker Borchert
Guest
Posts: n/a
 
      06-10-2012
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 <(E-Mail Removed)>
"I'm a mechanic, not a doctor." Volker Borchert <(E-Mail Removed)>
 
Reply With Quote
 
Jim Janney
Guest
Posts: n/a
 
      06-10-2012
bilsch <(E-Mail Removed)> writes:

> On 6/10/2012 7:02 AM, Jim Janney wrote:
>> bilsch<(E-Mail Removed)> 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-10-2012
(E-Mail Removed) (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«.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Nothing like this has ever been created, until now! sprint67 Computer Support 0 02-20-2005 03:18 AM
don't you ever, don't you ever, lower yourself, forgetting all your standards Jason ASP .Net Web Controls 0 07-06-2004 12:50 PM
Have you ever seen a traffic shape like this ? Chris Cisco 0 12-23-2003 04:19 AM



Advertisments