Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Question about costructors

Consider the following code:

class ConstructorExample {
  ConstructorExample() {
  System.out.println("random text blah....");
}
}

Even though I have a simple println statement inside my constructor does the compiler, by default, still add the call to super(); before my println statement?

1 Answer

Yes, it does. You can test it with some code like this

class A {
  A() {
    System.out.println("A being created."); 
  }
}

class B extends A {
  public B() {
    System.out.println("B being created.");
  }
}

public class Application {
  public static void main(String[] args)  {
    B b = new B();
  }
}

which will return:

A being created.
B being created.

@Pedro Cabral thanks for clearing that up man. I thought it did I just had to make sure.