Section 9 Constructor

本文深入探讨了Java中的堆和栈的概念,解释了实例变量与局部变量的区别,并详细阐述了构造器的使用方式及垃圾回收机制的重要性。通过解析Java内存分配与释放的过程,帮助开发者理解如何更有效地管理内存资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Stack and Heap:

Stack: method invocations and local variables.

Heap: all objects (instance variables inside), no matter the reference is a local or instance variable.


Instance variables: declared inside a class but not inside a method.

Local variables: declared inside a method, including method parameters.


Constructor:

The only outside way to invoke a constructor is with the keyword new.

You must write a no-arg constructor if you have written(overloaded) an arg constructor and want to use a no-arg constructor.

All the constructors in an object's inheritance tree must run when you make a new object.

When invoking a constructor, it first calls its super constructor way up to Object. So in every constructor, the compiler by default puts the no-arg super constructor super() as the first statement if you don't.

Superclass part is always completely built before the subclass can be constructed.

You can call this() in a constructor to invoke another constructor. A constructor can have a call to super() or this(), but never both.


Garbage Collection:

Your job is to make sure that you abandon objects when you're done with them.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值