标签:public java编程 静态块 name return 效果 port 可变 inf
import static java.lang.Integer.*; int max_value = MAX_VALUE; toHexString(max_value);
1 public static void main(String[] args) { 2 { 3 System.out.println("普通代码块-先声明"); 4 } 5 System.out.println("函数普通"); 6 { 7 System.out.println("普通代码块-后声明"); 8 } 9 }
普通代码块-先声明
函数普通
普通代码块-后声明
1 public class CodeBlock { 2 { 3 System.out.println("构造代码块-先声明-执行"); 4 } 5 public CodeBlock(){ 6 System.out.println("构造器执行"); 7 } 8 { 9 System.out.println("构造代码块-后声明-执行"); 10 } 11 public void plainFunc(){ 12 System.out.println("普通方法执行"); 13 } 14 public static void main(String[] args) { 15 System.out.println("main方法"); 16 CodeBlock cb1 = new CodeBlock(); 17 cb.plainFunc(); 18 CodeBlock cb2 = new CodeBlock(); 19 } 20 }
main方法 构造代码块-先声明-执行 构造代码块-后声明-执行 构造器执行 普通方法执行 构造代码块-先声明-执行 构造代码块-后声明-执行 构造器执行
1 class Code{ 2 { 3 System.out.println("Code的构造块"); 4 } 5 6 static{ 7 System.out.println("Code的静态代码块"); 8 } 9 10 public Code(){ 11 System.out.println("Code的构造方法"); 12 } 13 } 14 public class CodeBlock{ 15 { 16 System.out.println("CodeBlock的构造块"); 17 } 18 19 static{ 20 System.out.println("CodeBlock的静态代码块"); 21 } 22 23 public CodeBlock03(){ 24 System.out.println("CodeBlock的构造方法"); 25 } 26 27 public static void main(String[] args){ 28 System.out.println("CodeBlock的主方法"); 29 new Code(); 30 new Code(); 31 new CodeBlock(); 32 new CodeBlock(); 33 } 34 }
CodeBlock的静态代码块 CodeBlock的主方法 //这里还没有加载Code,所以先执行主方法,再执行Code的静态代码块 Code的静态代码块 Code的构造块 Code的构造方法 Code的构造块 Code的构造方法 CodeBlock的构造块 CodeBlock的构造方法
1 public class Parcel2 { 2 class Contents { 3 private int i = 11; 4 public int value() { return i; } 5 } 6 class Destination { 7 private String label; 8 Destination(String whereTo) { 9 label = whereTo; 10 } 11 String readLabel() { return label; } 12 } 13 public Destination to(String s) { 14 return new Destination(s); 15 } 16 public Contents contents() { 17 return new Contents(); 18 } 19 public void ship(String dest) { 20 Contents c = contents(); 21 Destination d = to(dest); 22 System.out.println(d.readLabel()); 23 } 24 public static void main(String[] args) { 25 Parcel2 p = new Parcel2(); 26 p.ship("Tasmania"); 27 Parcel2 q = new Parcel2(); 28 // Defining references to inner classes: 29 Parcel2.Contents c = q.contents(); 30 Parcel2.Destination d = q.to("Borneo"); 31 } 32 }
public class DotThis { void f() { System.out.println("DotThis.f()"); } public class Inner { public DotThis outer() { return DotThis.this; // A plain "this" would be Inner‘s "this" } } public Inner inner() { return new Inner(); } public static void main(String[] args) { DotThis dt = new DotThis(); DotThis.Inner dti = dt.inner(); dti.outer().f(); } }
public class DotNew { public class Inner {} public static void main(String[] args) { DotNew dn = new DotNew(); DotNew.Inner dni = dn.new Inner(); } }
Outer.Inner inner = new Outer.Inner(); inner.func();
interface content{ int func(); } System.out.println(new content(){ @Override public int func() { return 1; } }.func());
参考文章:
1、《java编程思想》
2、http://www.cnblogs.com/sophine/p/3531282.html
final、static、代码块、静态代码块、内部类、代码执行顺序
标签:public java编程 静态块 name return 效果 port 可变 inf
原文地址:http://www.cnblogs.com/uodut/p/7067195.html