2

The following code returns me an error saying:
"constructor call must be the first statment in a constructor."

I dont get it. The constructor in my code is the first statement. What am I doing wrong?

public class labelsAndIcons extends JFrame
{
    public labelFrame()
    {
        super( "Testing JLabel" );
    }
}

4 Answers 4

6

The constructors name must be the same as the class name, so change either change the class name to labelFrame or the constructor name to labelsAndIcons.

Example (note that usually the first letter is a capital letter in java):

public class LabelFrame extends JFrame {
    public LabelFrame() {
        super( "Testing JLabel" );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

do you mean

public class labelsAndIcons extends JFrame {
    public labelsAndIcons ()
    {
        super( "Testing JLabel" );
    }
}

Comments

0

Ideally your code should fail saying Invalid Method declartion because public labelFrame()

  • is neither a constructor (because constructor has the same name as the class name).
  • is neither proper method declaration.

Whatever change your code like this:

public class labelsAndIcons extends JFrame
{
  public labelsAndIcons ()
  {
     super( "Testing JLabel" );
  }
}

Comments

0

The constructors name must be the same as the class name. Let's look at this:

constructor call must be the first statement in a constructor  

The constructor word in constructor call references the super class's constructor which is the super();

The constructor word in in a constructor refers to your class's consucor that is : public labelsAndIcons()

so you need to narrow your code to this:

public class labelsAndIcons extends JFrame
{
  public labelsAndIcons ()
  {
     super( "Testing JLabel" );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.