Simple Java Question

Status
Not open for further replies.
When creating a swing window why does:

class ControlWindow extends JFrame
{



public void ControlWindow()
{
super("Hello");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

}

...produce the message 'call to super must be first statement in constructor' when compiling?

If you remove the 'void' keyword from the ControlWindow constructor it compiles fine. I thought a return type was compulsory (or void) why when you have the super() keyword it doesn't compile with 'void'?
 

mebeSajid

Ars Praefectus
4,022
Subscriptor
Change
code:<HR>
Code:
 class ControlWindow extends JFrame 
{
public void ControlWindow()
  {
     super("Hello");
     setSize(300,100);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
  }<P>}

to
code:<HR>
Code:
 class ControlWindow extends JFrame 
{
public ControlWindow()
  {
     super("Hello");
     setSize(300,100);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
  }<P>}

A constructor does not return a type.

Sajid
 

Sans

Wise, Aged Ars Veteran
144
Originally posted by Ding:
Constructors can't have return types, and super() makes little sense outside of a constructor.
Or at least I've never seen super() outside of a constructor.

You'll see super() used occasionally outside constructors. Usually happens when you override a method in a class you're extending but still want the original parent method to be called. For example:

code:<HR>
Code:
public class MoreStuff extends Stuff
{
  // constructors and other misc.<P>  public void doStuff()
  {
    System.out.println("Doing more stuff!");<P>    // have to use super() since simply calling doStuff() again will
    // result in recursion w/o a stop condition<P>    // This would probably print "Do stuff!" or similar
    super().doStuff();
  }
}
 

wb

Ars Legatus Legionis
25,208
Subscriptor
You'll see super() used occasionally outside constructors. Usually happens when you override a method in a class you're extending but still want the original parent method to be called.


Show me working source code that actually does that. So far as I know super can only be used as the first call in a constructor. Your example doesn't compile; I made a test case just to be sure.
 

Sans

Wise, Aged Ars Veteran
144
Try super not as a function call but as a attribute.

Oops. My fault. That's what I get for typing code in a web page :)

Here's an example snippet from a class I wrote to extend HashMap (I wanted to wrapper all objects I put in the map to store additional information for the object)

code:<HR>
Code:
public Object put(Object key, Object value)
{
  return(super.put( key, new ObjectWrapper(value, null, 0)));
}

Would like to claim credit for the idea, but actually found the idea in the article below. I just made a simplified version.
Javaworld Article
 

wb

Ars Legatus Legionis
25,208
Subscriptor
I just want to make it clear to everyone that there are two completely different cases where the super keyword gets used.

The super() method is used to explicitly invoke a base class constructor. The only time this is useful is when you explicitly want to call a base class constructor other than the default constructor (i.e. one that takes arguments). Otherwise, the default constructor super() is implicitly invoked anyway. This method cannot be called except for as the first call in a constructor.

The other usage of keyword is as a "shorthand" for ((BaseClassName) this) that you use to access base class methods or variables. I think the proper term is "shadowed" variables and methods. One trivial use for this is to differentiate between two variables or methods with the same name in both the base and derived classes.
 
Status
Not open for further replies.