Constructor and new operator in Java

前端 未结 1 1234
醉梦人生
醉梦人生 2020-12-06 19:36

To create a new object from a class Student in Java we use normally the following statement

Student std = new Student();

I\'ve

相关标签:
1条回答
  • 2020-12-06 20:26

    It's legal (though confusing) to have a method with the same name as the class, new removes any ambiguity. new indicates that the JVM should invoke the instance initialization method for the given class and argument list, and return the initialized object (referenced in the first (hidden) parameter of the initialization method).

    The designers of Java could probably have found an alternative syntax, but they had a design goal that any time memory was allocated on the heap it should be called out explicitly by requiring the keyword new. This probably seems weird now but most of Java's target audience was C and C++ programmers who were suspicious of garbage collection, this was meant to make sure developers didn't have memory allocated without their knowledge.

    0 讨论(0)
提交回复
热议问题