An anonymous object is an object that is not explicitly given a name--the anonymous object is used only once, and the anonymous object only opens up space in the heap memory, and there is no reference to the stack memory
/** * @authorYyx May 22, 2017*/ Public classAnonymousobject { Public Static voidMain (string[] args) {
//Anonymous object NewPerson ("Zhang San", 30). Tell (); NewPerson ("Zhang San", "+"). Tell (NewSchool ("Tsinghua")); }}classPerson {PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { Super(); This. Name =name; This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { //conditions for age constraints if(age >= 0 && <= 150) { This. Age =Age ; } } Public voidTell () {System.out.println ("Name:" + This. GetName () + ", Age:" + This. Getage ()); } Public voidTell (School SCL) {System.out.println ("Name:" + This. GetName () + ", Age:" + This. Getage ()+ ", School:" +scl.getname ()); }}classSchool {PrivateString name; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicSchool (String name) {Super(); This. Name =name; }}
Anonymous objects are essentially passed as arguments to other class instantiation objects in real-world development.
Java Anonymous objects