实例变量的初始化方法

第一种:通过构造函数进行初始化。

第二种:通过声明实例字段初始化。

第三种:通过对象代码块初始化。

通过构造函数进行初始化方法

通过构造函数进行对象初始化,必须在类中声明一个带参数的构造函数。从而通过类创建实例的同时对实例变量进行初始化。注:如果没有声明带参数的构造函数,调用默认构造函数,默认构造函数也对实例变量进行了默认初始化。例如:

 package com.java.test;

 class Product {
private int id;
private String name; public Product() { } public Product(int id, String name) {
this.id = id;
this.name = name;
}
public String toString()
{
return id+","+name;
} public void print()
{
System.out.println(toString());
}
} public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();
c.print();
//结果是0,null
Product s=new Product(10,"apple");
s.print();
//结果是10,apple }
}

通过声明实例字段进行初始化方法

通过实例变量声明实例变量就是在创建类的时候,对实例变量进行初始化。例如:

 class SomeClass
{
static boolean b;
static byte by;
static char c;
static double d;
static float f;
static int i;
static long l;
static short s;
static String st;
}

初始化结果为

false
0
\u0000
0.0
0.0
0
0
0
null

通过对象代码块进行初始化方法

对象代码块初始化是在类中声明代码块来进行声明实例变量。对象代码块的执行是在对象执行构造函数前执行。对象代码块前没有static关键字,加static关键字就变为类代码块。下面通过一个例子来说明:

 package test;

 class Product {
private int id;
private String name; public Product() { } public Product(int id, String name) {
this.id = id;
this.name = name;
}
public String toString()
{
return id+","+name;
}
21 {
22 name="Sharplee";
23 }
public void print()
{
System.out.println(toString());
} 29 {
30 System.out.println("id is "+id);
31 System.out.println("name is "+name);
32 } } public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();//id is0 name isSharplee
c.print();//0,Sharplee Product s=new Product(10,"apple");//id is0 name isSharplee
s.print();//10,apple }
}

通过该代码能够看出代码块执行也是从上到下的顺序,并且代码块执行是在构造函数之前。代码块的出现,是便于匿名类来使用的。匿名类是不创建构造函数的。因此在初始化变量的时候,可以使用代码块。

类代码块

类代码块就是在加载类的时候进行初始化。例如:

 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{
price=100;
System.out.println("the price is:"+price);
}
public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product c=new Product();
c.print(); Product s=new Product(10,"apple",300);
s.print(); }
}

类代码块以及块代码块的区别

类代码块无论创建多少个对象都只初始化一次,而对象代码块在创建的对象的时候都执行。

类代码块初始化必须在前面加关键字static,而对象代码块则不用加。

类代码块只能使用类变量进行初始化以及在代码块中声明变量,而对象代码块则没有限制。

 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{

price=100;
System.out.println("the price is:"+price);
}
static
{
price++;
} public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product p=null;
Product t=new Product();
Product c=new Product(); }
}
 package test;

 class Product {
private static int price;
private int id;
private String name; public Product() { }
static
{
int ss=10;
price=100;
System.out.println("the price is:"+price);
} {
price++;
} public Product(int id, String name,int price) {
this.id = id;
this.name = name;
this.price=price;
}
public String toString()
{
return id+","+name+","+price;
}
{
name="Sharplee";
}
public void print()
{
System.out.println(toString());
} {
System.out.println("id is "+id);
System.out.println("name is "+name);
System.out.println(price);
} } public class TestProduct
{
public static void main(String[]args)
{
Product p=null;
Product t=new Product();
Product c=new Product(); }
}

对象创建的执行顺序

总结:对象创建首先进行类实例变量以及类代码块的初始化。接着是类的父类的对象代码块执行,类的父类构造函数执行,最后执行类中代码块以及类的构造函数。

java学习之实例变量初始化的更多相关文章

  1. Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序

    先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...

  2. Java实例变量初始化

    由一道面试题所想到的--Java实例变量初始化 时间:2015-10-07 16:08:38      阅读:23      评论:0      收藏:0      [点我收藏+] 标签:java   ...

  3. java静态类、静态方法、静态代码块,静态变量及实例方法,实例变量初始化顺序及内存管理,机制

    1.当一个类被第一次使用时,它需要被类加载器加载,而加载过程涉及以下两点: (1)在加载一个类时,如果它的父类还未被加载,那么其父类必须先被加载: (2)当类加载到内存之后,按照在代码中的出现顺序执行 ...

  4. Java类变量、实例变量的初始化顺序

    题目: public class InitTest{ public static int k = 0; public static InitTest t1 = new InitTest("t ...

  5. Java构造方法、成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的?

    [Java笔试真题]:构造方法.成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的? [解答]:当类第一次被加载的时候,静态变量会首先初始化,接着编译器会把实例变量初始化为默认值,然后执行构 ...

  6. Java类、实例的初始化顺序

    今晚是阿里巴巴 2013 校园招聘的杭州站笔试.下午匆忙看了两张历年试卷,去现场打了瓶酱油. 题目总体考察点偏基础,倒数第二题(Java 附加题)比较有趣,考察了 Java 初始化机制的细节,在此摘录 ...

  7. Java类变量和成员变量初始化过程

    一.类的初始化 对于类的初始化:类的初始化一般只初始化一次,类的初始化主要是初始化静态成员变量. 类的编译决定了类的初始化过程. 编译器生成的class文件主要对定义在源文件中的类进行了如下的更改: ...

  8. 0020 Java学习笔记-面向对象-变量

    变量分为哪些 成员变量:类里面,方法外面定义的变量 实例变量:没有用static修饰的变量,属于对象:存在期:创建实例-销毁实例:作用域:与该实例的生存范围相同 类变量:用static修饰的变量,属于 ...

  9. oc实例变量初始化方法

    1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...

随机推荐

  1. python的单元测试框架

    1.unittest是Python内置的标准类库.它的API跟Java的JUnit..net的NUnit,C++的CppUnit很相似.   通过继承unittest.TestCase来创建一个测试用 ...

  2. [转]awk使用手册

    awk 手册 简体中文版由bones7456 (bones7456@gmail.com)整理. 原文:应该是 http://phi.sinica.edu.tw/aspac/reports/94/940 ...

  3. 一个table插件,用于bootstrap开发

    最近项目中改用bootstrap,可以给的通用table,写的有点死,id名称是固定的,那一个页面两个table如何做呢? ok,模仿着别人的代码,写了一个,整体代码如下: ; (function(f ...

  4. SQL Server 2012不支持Microsoft Visual Studio Test Controller 2010

    折腾了一个上午, 发现Test Controller怎么都连不上SQL. 能尝试的都尝试了, 觉得应该看看是不是有不支持的问题.   找到了这篇. TFS 2010 will not support ...

  5. ECharts学习总结(二):标签式单文件引入echarts的方法

    下载好echarts的库文件.然后在script里面引入. //from echarts example <body> <div id="main" style= ...

  6. idea maven 报-source 1.5 中不支持 diamond 运算符

    需要修改 project setting 中的

  7. [Transducer] Make Transducer works for Iteratable collection and Object

    We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in ...

  8. linux性能监控命令

    vmstat 可以用来监控虚拟内存.可对操作系统的虚拟内存.IO.CPU等多个指标的整体情况进行监视. Linux系统的内存分为物理内存和虚拟内存两种.物理内存是真实的,也就是物理内存条上的内存.而虚 ...

  9. Vagrant up Warning: Authentication failure. Retrying...

    Vagrant up Warning: Authentication failure. Retrying... Vagrant up Warning: Authentication failure. ...

  10. IO核心代码