Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15180

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

From Jim Janney <jjanney@shell.xmission.com>
Newsgroups comp.lang.java.programmer
Subject Re: It doesn't like 'super' where ever I put it.
Date 2012-06-10 11:24 -0600
Organization slowly at first, then all at once
Message-ID <ydnpq97gjwk.fsf@shell.xmission.com> (permalink)
References <jr27ce$7uo$1@dont-email.me> <ydny5nvgt9d.fsf@shell.xmission.com> <jr2aet$pv7$1@dont-email.me> <ydntxyjgmkb.fsf@shell.xmission.com>

Show all headers | View raw


Jim Janney <jjanney@shell.xmission.com> writes:

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

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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 06:27 -0700
  Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 13:49 +0000
    Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 07:08 -0700
      Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-10 11:34 -0400
        Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 12:58 -0700
          Re: It doesn't like 'super' where ever I put it. markspace <-@.> - 2012-06-10 13:43 -0700
            Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-11 21:01 -0700
      Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 15:44 +0000
    Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-10 20:39 +0200
      Re: It doesn't like 'super' where ever I put it. "Gavino" <invalid@invalid.invalid> - 2012-06-11 20:13 +0200
        Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-11 22:51 +0200
          Re: It doesn't like 'super' where ever I put it. "Gavino" <invalid@invalid.invalid> - 2012-06-12 00:45 +0200
            Re: It doesn't like 'super' where ever I put it. Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-11 22:53 +0000
              Re: It doesn't like 'super' where ever I put it. Lew <lewbloch@gmail.com> - 2012-06-11 16:42 -0700
            Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-11 19:53 -0400
              Re: It doesn't like 'super' where ever I put it. Lew <lewbloch@gmail.com> - 2012-06-11 17:38 -0700
                Re: It doesn't like 'super' where ever I put it. "John B. Matthews" <nospam@nospam.invalid> - 2012-06-11 23:14 -0400
                Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-12 18:21 +0200
      Re: It doesn't like 'super' where ever I put it. Robert Klemme <shortcutter@googlemail.com> - 2012-06-11 22:50 +0200
  Re: It doesn't like 'super' where ever I put it. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-10 09:59 -0400
  Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 08:02 -0600
    Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 07:20 -0700
      Re: It doesn't like 'super' where ever I put it. Leif Roar Moldskred <leifm@dimnakorr.com> - 2012-06-10 09:49 -0500
        Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:15 -0700
      Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 10:26 -0600
        Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 11:24 -0600
        Re: It doesn't like 'super' where ever I put it. Jim Janney <jjanney@shell.xmission.com> - 2012-06-10 11:38 -0600
          Re: It doesn't like 'super' where ever I put it. v_borchert@despammed.com (Volker Borchert) - 2012-06-10 18:17 +0000
            Re: It doesn't like 'super' where ever I put it. Lew <noone@lewscanon.com> - 2012-06-10 12:26 -0700
        Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:17 -0700
  Re: It doesn't like 'super' where ever I put it. Roedy Green <see_website@mindprod.com.invalid> - 2012-06-10 11:54 -0700
    Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-10 13:24 -0700
  Re: It doesn't like 'super' where ever I put it. Lew <noone@lewscanon.com> - 2012-06-10 13:56 -0700
    Re: It doesn't like 'super' where ever I put it. bilsch <bilsch01@gmail.com> - 2012-06-11 20:55 -0700
  Re: It doesn't like 'super' where ever I put it. Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-11 16:41 -0700

csiph-web