What happens when you create a new object?

前端 未结 10 1086
一个人的身影
一个人的身影 2021-02-10 18:53

Ok, so what happens when you do this.

A a1=new A();

A a2=new A();

A a3=new A();

I upload two pictures on how I imagine it being like. Can you

10条回答
  •  有刺的猬
    2021-02-10 19:25

    This is a variable declaration;

    A a1;
    

    You declare a new variable called a1 of type A.

    a1 = new A();
    

    You assign to a1 the value that results from new A(), which is a new object of type A.

    You can do both things at once as an initialization:

    A a1 = new A();
    

    Each time you call your class constructor, you get a different and separated instance of your class. No instances know about the others, they lie in different regions of memory.

    By the way, those are very very basics of programming, so get a good reading and happy learning.

提交回复
热议问题