java学习之实例变量初始化
实例变量的初始化方法
第一种:通过构造函数进行初始化。
第二种:通过声明实例字段初始化。
第三种:通过对象代码块初始化。
通过构造函数进行初始化方法
通过构造函数进行对象初始化,必须在类中声明一个带参数的构造函数。从而通过类创建实例的同时对实例变量进行初始化。注:如果没有声明带参数的构造函数,调用默认构造函数,默认构造函数也对实例变量进行了默认初始化。例如:
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学习之实例变量初始化的更多相关文章
- Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序
先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...
- Java实例变量初始化
由一道面试题所想到的--Java实例变量初始化 时间:2015-10-07 16:08:38 阅读:23 评论:0 收藏:0 [点我收藏+] 标签:java ...
- java静态类、静态方法、静态代码块,静态变量及实例方法,实例变量初始化顺序及内存管理,机制
1.当一个类被第一次使用时,它需要被类加载器加载,而加载过程涉及以下两点: (1)在加载一个类时,如果它的父类还未被加载,那么其父类必须先被加载: (2)当类加载到内存之后,按照在代码中的出现顺序执行 ...
- Java类变量、实例变量的初始化顺序
题目: public class InitTest{ public static int k = 0; public static InitTest t1 = new InitTest("t ...
- Java构造方法、成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的?
[Java笔试真题]:构造方法.成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的? [解答]:当类第一次被加载的时候,静态变量会首先初始化,接着编译器会把实例变量初始化为默认值,然后执行构 ...
- Java类、实例的初始化顺序
今晚是阿里巴巴 2013 校园招聘的杭州站笔试.下午匆忙看了两张历年试卷,去现场打了瓶酱油. 题目总体考察点偏基础,倒数第二题(Java 附加题)比较有趣,考察了 Java 初始化机制的细节,在此摘录 ...
- Java类变量和成员变量初始化过程
一.类的初始化 对于类的初始化:类的初始化一般只初始化一次,类的初始化主要是初始化静态成员变量. 类的编译决定了类的初始化过程. 编译器生成的class文件主要对定义在源文件中的类进行了如下的更改: ...
- 0020 Java学习笔记-面向对象-变量
变量分为哪些 成员变量:类里面,方法外面定义的变量 实例变量:没有用static修饰的变量,属于对象:存在期:创建实例-销毁实例:作用域:与该实例的生存范围相同 类变量:用static修饰的变量,属于 ...
- oc实例变量初始化方法
1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...
随机推荐
- COGS 08-备用交换机 题解——S.B.S.
8. 备用交换机 ★★ 输入文件:gd.in 输出文件:gd.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] n个城市之间有通讯网络,每个城市都有通讯交换机,直 ...
- IP addr命令
我们都知道Windows上查看IP地址是ipconfig, Linux上是ifconfig,但是Linux上还有一个命令叫ip addr可以查看IP地址. 如上图所示命令显示了机器上的所有网卡,大部分 ...
- JavaScript逻辑and、or、not运算符详解
一.AND详解: 在JavaScript中,逻辑 AND 运算符用双和号(&&)表示. 需要说明的是:逻辑AND运算的运算数可以是任何类型的,不止是Boolean值,如果某个运算数不是 ...
- NPOI导出Excel时出现错误“Maximum column number is 255”
此错误并不是NPOI的问题,而是Excel的问题,原因是Excel的最大列255,所以导出的列超过255时就会出现此问题 但Excel2007以上并没有此问题,因为2007以上的列已经增加到16384 ...
- STL - 常用关联容器代码 - set & multiset
代码如下: /* 5. set & multiset */ set<string> cities{ "Braunschweig", "Hanover& ...
- 根据wsdl文件生成java代码
具体操作步骤: 1. 运行cmd命令 输入 C:\jdk-1_6_0_31\bin (根据自己的JDK的安装位置而定) 2. 新建gen.bat文件 3. gen.bat文件内容为:wsimpo ...
- PHP 杂项函数
PHP 杂项 函数 PHP 杂项函数简介 我们把不属于其他类别的函数归纳到杂项函数类别. 安装 杂项函数是 PHP 核心的组成部分.无需安装即可使用这些函数. Runtime 配置 杂项函数的行为受 ...
- Burning widget
This is a widget that we can see in Nero, K3B, or other CD/DVD burning software. #!/usr/bin/python # ...
- Vagrant up Warning: Authentication failure. Retrying...
Vagrant up Warning: Authentication failure. Retrying... Vagrant up Warning: Authentication failure. ...
- 金蝶K3,域环境中,无本地用户管理员权限的域用户如何设置注册表权限?
如果该用户是属于Power Users组:只需要给用户添加注册表中的HKEY_LOCAL_MACHINE的SOFTWARE完全控制的权限.(勾选允许父项的继承权限和传播到该对象和所有子对象) 如果该用 ...