java 静态块的作用域_java 学习笔记(一)

对象

new 出来的对象都是保存在堆内存中的

不是使用 new 创建变量,而是使用一个“自动”变量。 这个变量直接存储"值",并置于栈内存中,因此更加高效。

基本类型有自己对应的包装类型,如果你希望在堆内存里表示基本类型的数据,就需要用到它们的包装类

或者使用 自动装箱

Character ch = 'x';

d5b2cf85f79e05e57ff58efbe09bb20c.png

对象作用域

Java 对象与基本类型具有不同的生命周期。当我们使用 new 关键字来创建 Java 对象时,它的生命周期将会超出作用域。

基本类型默认值

9a851b834c479f4f3377860157f7df76.png

无参构造器

一旦你显式地定义了构造器(无论有参还是无参),编译器就不会自动为你创建无参构造器。

this

this就是调用改方法的对象,就像是

81d46bb5121a0166fc79e781b14a1c6e.png

或者用去在构造器中调用其他其他构造器

static 方法中不会存在 this。你不能在静态方法中调用非静态方法

static只能用于属性(字段、域) 不能用于局部变量

静态初始化顺序

class Bowl {

Bowl(int marker) {

System.out.println("Bowl(" + marker + ")");

}

void f1(int marker) {

System.out.println("f1(" + marker + ")");

}

}

class Table {

static Bowl bowl1 = new Bowl(1);

Table() {

System.out.println("Table()");

bowl2.f1(1);

}

void f2(int marker) {

System.out.println("f2(" + marker + ")");

}

static Bowl bowl2 = new Bowl(2);

}

class Cupboard {

Bowl bowl3 = new Bowl(3);

static Bowl bowl4 = new Bowl(4);

Cupboard() {

System.out.println("Cupboard()");

bowl4.f1(2);

}

void f3(int marker) {

System.out.println("f3(" + marker + ")");

}

static Bowl bowl5 = new Bowl(5);

}

public class StaticInitialization {

public static void main(String[] args) {

System.out.println("main creating new Cupboard()");

new Cupboard();

System.out.println("main creating new Cupboard()");

new Cupboard();

table.f2(1);

cupboard.f3(1);

}

static Table table = new Table();

static Cupboard cupboard = new Cupboard();

}

==输出:==

Bowl(1)

Bowl(2)

Table()

f1(1)

Bowl(4)

Bowl(5)

Bowl(3)

Cupboard()

f1(2)

main creating new Cupboard()

Bowl(3)

Cupboard()

f1(2)

main creating new Cupboard()

Bowl(3)

Cupboard()

f1(2)

f2(1)

f3(1)

一个java文件里面只能有一个public class

静态属性安申明顺序执行,之后再是构造器

类中的static只会初始化一次

非静态初始化

static{

/* 静态代码块*/

}

{

/*非静态代码块*/

}

==非静态代码块在每次生成对象是都会执行==

// housekeeping/Mugs.java

// Instance initialization

class Mug {

Mug(int marker) {

System.out.println("Mug(" + marker + ")");

}

}

public class Mugs {

Mug mug1;

Mug mug2;

{ // [1]

mug1 = new Mug(1);

mug2 = new Mug(2);

System.out.println("mug1 & mug2 initialized");

}

Mugs() {

System.out.println("Mugs()");

}

Mugs(int i) {

System.out.println("Mugs(int)");

}

public static void main(String[] args) {

System.out.println("Inside main()");

new Mugs();

System.out.println("new Mugs() completed");

new Mugs(1);

System.out.println("new Mugs(1) completed");

}

}

==输出:==

Inside main

Mug(1)

Mug(2)

mug1 & mug2 initialized

Mugs()

new Mugs() completed

Mug(1)

Mug(2)

mug1 & mug2 initialized

Mugs(int)

new Mugs(1) completed

数组的创建

int[] a1 = {1, 2, 3, 4, 5};

int[] a = new int[rand.nextInt(20)];

或者

new String[] {"fiddle", "de", "dum"}

==数组的定义==

int[] a

int a[]

可变参数列表

static void printArray(Object... args)

public class NewVarArgs {

static void printArray(Object... args) {

for (Object obj : args)

System.out.print(obj + "!! ");

System.out.println();

}

public static void main(String[] args) {

// Can take individual elements:

printArray(47, (float) 3.14, 11.11);

printArray(47, 3.14F, 11.11);

printArray("one", "two", "three");

printArray(new A(), new A(), new A());

// Or an array:

printArray((Object[]) new Integer[] { 1, 2, 3, 4 });

printArray(); // Empty list is OK

}

}

==输出:==

cf0ab9edb9cc42b3d1706241bbbfe2ba.png

==如果把数组传入可变对象参数中,该方法会把它们当作可变参数列表来接受。==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值