实例变量的初始化方法

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

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

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

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

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

 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. SQL中truncate table和delete的区别

    truncate table table_name delete from table_name drop table table_name truncate table在功能上与不带where子句的 ...

  2. pidera安装node.js(树莓派)

    1. 下载node.js wget http://nodejs.org/dist/v0.10.5/node-v0.10.5-linux-arm-pi.tar.gz tar -xzvf node-v0. ...

  3. oracle分析函数之windowing_clause--rows

    Some analytic functions allow the windowing_clause. In the listing of analytic functions at the end ...

  4. go语言基础之局部变量特点

    1.局部变量特点 示例1: package main import "fmt" func main() { //定义在{}里面的变量就是局部变量,只能在{}里面有效 //执行到定义 ...

  5. go语言基础之递归函数的调用流程

    一.递归函数的调用流程 package main //必须 import "fmt" func test(a int) { if a == 1 { //函数终止调用的条件,非常重要 ...

  6. 查看网络IP连接

  7. HDU 2897 邂逅明下 (博弈)

    题意: 给你n.p.q,每次操作是令n减小 [p, q]区间中的数,当n < p时必须全部取完了,取完最后一次的人算输,问先手必胜还是必败. 解题思路: 这种非常类似巴什博弈,可以找出必胜区间和 ...

  8. 从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?

    这一章节我们继续类型擦除的话题,我们将通过对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题? 1.数组 package com.ray.ch13; public class Test { pub ...

  9. Simple drag and drop

    In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) ...

  10. hunnu11546:Sum of f(x)

    Problem description   令f(x)为x的全部约数之和,x的约数即能够被x整除的数.如f(24)=1+2+3+4+6+8+12+24=60),求 f(l) + f(l + 1) + ...