There is a simple object instantiation:
Sample s = new Sample();and the anonymous object instantiation:
new Sample();The anonymous object is created and dies instantaneously. But, still with anonymous objects work can be extracted before it dies like calling a method using the anonymous object:
new Sample().display()The anonymous object can be used only once in the program. We can’t use twice or more as the anonymous object dies immediately after doing its assigned task.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));if an object would be used once only, you can use an anonymous object instantiation instead of object instantiation.
Remarks
Text provided above does not means is the model answer, and it's was posted here for an reference to somebody who interested the related knowledge only. if there is something incorrect, please leave a comment so that i can correct it or for others viewer to have an better reference, if you want know more about this topic and where is the question, concept or answer from, you can reference from information provided under the reference sub-title.
Reference:
Introduction to Java Programming, Y. Daniel Liang
http://www.austincc.edu/baldwin/ITSE2321LectureNotesAndSlides/LectureNotes/Lecture05/Lecture05.htm
No comments :
Post a Comment