![]() |
It doesn't like 'super' where ever I put it.
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) |
Re: It doesn't like 'super' where ever I put it.
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> |
Re: It doesn't like 'super' where ever I put it.
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 |
Re: It doesn't like 'super' where ever I put it.
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.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 |
Re: It doesn't like 'super' where ever I put it.
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. |
Re: It doesn't like 'super' where ever I put it.
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.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? |
Re: It doesn't like 'super' where ever I put it.
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 |
Re: It doesn't like 'super' where ever I put it.
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> |
Re: It doesn't like 'super' where ever I put it.
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 |
Re: It doesn't like 'super' where ever I put it.
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«. |
| All times are GMT. The time now is 08:03 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.