Home > Back-end >  [10.6] creates an instance of object in two ways.
[10.6] creates an instance of object in two ways.

Time:10-07

Create an instance object:

Method a : manually create and instantiate,
1, to create an object,
The Class Class=null;

2, use the new keyword to instantiate the class,
Class=new class ();

Usually written: Class Class=new Class ();


Method of 2 : the use of other classes to return an instance of the object,
1, to create an object, used to receive the returns an instance of the object,
The Iterator Iterator=null;

2, can call a method returns the instance objects, such as: the list. The Iterator ();
The iterator=list. The iterator ();

Usually written: Iterator Iterator=list. The Iterator ();

[this is the 20th day I learn Java, when using iterators, instantiation of the process is different with usual, confused for a moment, I am the kind of person who don't understand just don't remember, share with you, so write hope to help beginners!]

CodePudding user response:

You return values of the iterator is method not create objects
Two methods to create objects is
1 call new method to construct
2 using the reflection, call Class object. NewInstance () or a Class object. GetDeclaredConstructor (). The newInstance ()

CodePudding user response:

reference 1st floor qybao response:
you return values of the iterator is method not create objects
Two methods to create objects is
1 call new method to construct
2 using the reflection, call Class object. NewInstance () or a Class object. GetDeclaredConstructor (). The newInstance ()

The Iterator () source is the return of new Iterator (),

New in the source code, object is created,
Reflection I haven't learned, because here call iterator, instantiation of writing is different,
Maybe I should correct once, is to use the new create objects of two kinds of writing,

CodePudding user response:



That's not to say a different way of creating objects, at most can calculate different writing XD

  • Related