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
On 6/10/2012 8:34 AM, Eric Sosman wrote:
> 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.
>


Thanks for the explanation. I have some Java books. They are kind of
vague on the subject.
 
Reply With Quote
 
 
 
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
On 6/10/2012 7:49 AM, Leif Roar Moldskred wrote:
> bilsch<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
On 6/10/2012 9:26 AM, Jim Janney wrote:
> 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.
>

Thanks for the info.
 
Reply With Quote
 
bilsch
Guest
Posts: n/a
 
      06-10-2012
On 6/10/2012 11:54 AM, Roedy Green wrote:
> On Sun, 10 Jun 2012 06:27:41 -0700, bilsch<(E-Mail Removed)> 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.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      06-10-2012
On 6/10/2012 12:58 PM, bilsch wrote:
> Thanks for the explanation. I have some Java books. They are kind of
> vague on the subject.



I like to keep in mind lists of books that aren't good Java books, as
well as those that are good, just to be able to recommend them (or not)
when questions are asked on this list. Care to let us know which books
you find wanting?

FYI, I own and recommend Learning Java, published by O'Reilly, third
edition. I don't recall exactly where I learned that constructors have
no return type keyword, but I'm sure it's in there.

I also recommend the Java tutorial on Oracle's website. It's not
in-depth, but it often is useful to answer specific questions.

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

"Constructor declarations look like method declarations—except that they
use the name of the class and have no return type"

 
Reply With Quote
 
Lew
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?
>
> 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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Gavino
Guest
Posts: n/a
 
      06-11-2012
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> Robert Klemme <(E-Mail Removed)> writes:
> >On 10.06.2012 19:10, Stefan Ram wrote:
> >> http://www.velocityreviews.com/forums/(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«.

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


It can't because the superclass (JFrame) does not have a constructor without
arguments.



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-11-2012
On 10.06.2012 20:51, Stefan Ram wrote:
> Robert Klemme<(E-Mail Removed)> writes:
>> On 10.06.2012 19:10, Stefan Ram wrote:
>>> (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«.

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


Only that this won't be done for non constructors which is what the
method was, as has been pointed out
public void CalcFrame1() { ... }

Note that super(...) is always a constructor call because method calls
read super.method(...).

Granted, the error message is probably not the most telling. Eclipse says
Constructor call must be the first statement in a constructor
CalcFrame1.java /c.l.j.p/src line 8 Java Problem

Which is certainly clearer.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-11-2012
On 11.06.2012 20:13, Gavino wrote:
> "Stefan Ram"<(E-Mail Removed)-berlin.de> wrote in message
> news:(E-Mail Removed)-berlin.de...
>> Robert Klemme<(E-Mail Removed)> writes:
>>> On 10.06.2012 19:10, Stefan Ram wrote:
>>>> (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«.
>>> 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()«.

>
> It can't because the superclass (JFrame) does not have a constructor without
> arguments.


Also wrong.

http://docs.oracle.com/javase/6/docs...e.html#JFrame()

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Gavino
Guest
Posts: n/a
 
      06-11-2012
"Robert Klemme" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 11.06.2012 20:13, Gavino wrote:
>> It can't because the superclass (JFrame) does not have a constructor without
>> arguments.


> Also wrong.


> http://docs.oracle.com/javase/6/docs...e.html#JFrame()


My apologies.
I don't know where I got that erroneous idea from, but somehow I've been carrying it
around in my head for the last 12 years.
I should have checked the JavaDocs instead of relying on badly remembered information.

Now I'm equally confused about the wording of the error message.




 
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