Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A {
- static String s = "AAA";
- A() {
- System.out.println("#5 class A constructor");
- }
- static {
- System.out.println("#1 class A static {}");
- s = s + "BBB";
- }
- {
- System.out.println("#2 class A {}");
- s = "AAABBB";
- //s = "Hello world";
- }
- }
- class B extends A {
- B() {
- System.out.println("#6 class B constructor");
- }
- static {
- System.out.println("#3 class B static {}");
- s = s + "BBBAAA";
- }
- {
- System.out.println("#4 class B {}");
- System.out.println("s = " + s);
- }
- }
- public class OCA16 {
- public static void main(String[] args) {
- B b = new B();
- }
- }
- /*
- NetBeans IDE 8.2
- run:
- #1 class A static {}
- #3 class B static {}
- #2 class A {}
- #5 class A constructor
- #4 class B {}
- s = AAABBB
- #6 class B constructor
- BUILD SUCCESSFUL (total time: 0 seconds)
- */
- /*
- S M Athiqur RahmanC , C++ , C# , Java, SQL , VB. net, Python , MVC , Web Programming
- 11 小時 ·
- Why answer is : AAABBB? any explanation from experts...
- https://www.facebook.com/photo.php?fbid=2340845999536149&set=gm.2489663694438490&type=3&theater&ifg=1
- 郭翔宇
- 6 小時
- 大家好,小弟是程式初學者。
- 最近在其他facebook社團看見有人詢問Java相關問題。
- 設一個 類別A,再設一個 類別B,
- 在 類別A 裡面宣告一個 static變數,
- 然後分別在 static{} 跟 {} 裡面修改變數值。
- 接著 類別B 繼承 類別A。
- 1.
- 請問各位,
- 程式執行出來的結果 "AAABBB" 是怎麼生出來的?
- 2.
- static{} 跟 {} 到底差在哪裡?
- 查詢這些東西的 google搜尋關鍵字 是?
- 附上 Java程式碼
- https://www.facebook.com/groups/1403852566495675/permalink/2263345963879660/
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement