Creating multiple objects in the Java language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.