Key points of knowledge
1. Create Multiple objects
Dog D1 = new Dog ();
Dog D2 = new Dog ();
Object name Variable name (d1, D2) The entity placed in the stack memory object is placed in heap memory
2. The difference between an object and an object
Dog D1 = new Dog ();
Dog D2 = new Dog ();
Each object generated consumes a block of space in the heap memory and does not occupy the same address in the heap memory
Declared variables open up a space in the stack memory
3. How to create and use anonymous objects
You can call the object's method without defining the object's reference name, such an object is called an anonymous object such as
New Dog (). Jump ();
Anonymous objects
4. Features of empty objects
Empty objects have names in the stack memory and no objects in heap memory
Scanner s = null;
Empty Object
S.student ()//Call function.
Compile yes, run an error.
The code is as follows:
Class Students
{
String name;
String School;
int age;
void Introduce ()
{
SYSTEM.OUT.PRINTLN ("Student's name:" + name);
System.out.println ("The name of the school:" + school);
System.out.println ("Age of the pupils:" + ages);
}
}
class Test2
{
public static void main (String[]a RGS)
{
&NBSP ; Students S1 = new Students ();
Students s2 = new St Udents ();
S1.name = "Zhang San";
S1.school = "Tsinghua";
s1.age = 20;
S2.name = "John Doe"; br> S2.school = "PKU";
s2.age =
System.out.println ("S1.name:" + s1.name);
System.out.println ("S1.school:" + s1.school);
System.out.println ("S2.age:" + s2.age);
S1.introduce ();
System.out.println ("--------------");
S2.introduce ();
}
}
Creating multiple objects in the Java language