Java类实例化过程
2016-02-24 22:42
459 查看
To summarize the process of creating an object, consider a class called Dog:
1.Even though it doesn’t explicitly use the static keyword, the constructor is actually a static method. So the first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must
locate Dog.class, which it does by searching through the classpath.
2.
As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.
3.
When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
4.
This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.
5.
Any initializations that occur at the point of field definition are executed.
6.
Constructors are executed. As you shall see in the Reusing Classes chapter, this might actually involve a fair amount of activity, especially when inheritance is involved.
相关文章推荐
- Java EE 经验
- Java学习之枚举
- Spring-03-AOP配置
- 使用Ratpack和Spring Boot打造高性能的JVM微服务应用
- Rxjava基础
- 随堂笔记160224基础语法
- Struts2-工作原理
- java单例模式singleton回顾,如何实现单例模式
- JavaIO之-BIO(同步阻塞线程)
- Java的最佳实践
- Java反射机制分析指南
- JAVA语法基础 3
- 最全面的Java多线程用法解析
- Spring JTA事务配置JOTM
- Java实现配置加载机制
- Java新手问题集锦汇总
- JAVA SE 学习笔记
- java多线程-CyclicBarrier
- 【JAVA】19、多维数组
- Spring整合RabbitMQ进行消息队列开发