0

I'm a rudimentary java programmer. I got a question about constructor and new operator, as seen in topic.

I got the major concept of 'constructor' and 'new' operator, but can't get the main idea why we really needs 'new' operator because the essential function of instantiation can be performed only by constructor.

class Parent {

    // some fields here.

    Parent(){
       this.child = 0;
    }  

}

If class Parent is defined like above, when we instantiate the Parent class, we writes some codes like below.

Parent parent1 = new Parent();

But here, if we made constructor itself can create new instance, then isn't there any need to use 'new' operator?

Parent parent1 = Parent();

I know that it would cause some compile errors, but my question is why Java-Creator seperated the role of instantiation into two - constructor and new at the very first time!

I expect that there must be some appropriate reason for this.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Now.Zero
  • 1,147
  • 1
  • 12
  • 23
  • 2
    `new Foo(…)` works when `Foo` is a class, `Foo(…)` works when `Foo` is a function. (They might even be in different namespaces in Java? I don’t remember.) It’s nice to distinguish the two, which is why there’s the convention of uppercase letters for classes. – Ry- Dec 25 '17 at 05:39
  • 1
    Also the new keyword allows you to create things like arrays (`new int[10]`) where there's no function-like constructor visible. – azurefrog Dec 25 '17 at 05:40

0 Answers0