class init & object creation

class init & object creation in inheritance

example:

execute SelfBall.java 's main () function, then get the result.

GrandpaBall.java

/**
 * grandpa ball
 * @author eric
 * @date 2010-3-19  Morning  11:43:42
 */
public class GrandpaBall {
        // static fields
        protected static int gx = gxInit();
        protected static int gy;
        // non-static fields
        protected int ga = gaInit();
        // static block
        static {
                System.out.println("grandpa static block init.");
        }
        // non-static block
        {
                System.out.println("grandpa non-static block init.");
        }

        // constructor
        public GrandpaBall() {
                System.out.println("grandpa constructor execute.");
        }

        private static int gxInit() {
                System.out.println("grandpa static field init.");
                return 1;
        }

        private int gaInit() {
                System.out.println("grandpa non-static field init.");
                return 1;
        }
}

FatherBall.java

/**
 * father ball
 * @author eric
 * @date 2010-3-19  Morning  11:44:01
 */
public class FatherBall extends GrandpaBall {
        // static fields
        protected static int fx = fxInit();
        protected static int fy;
        // non-static fields
        protected int fa = faInit();
        // static block
        static {
                System.out.println("father static block init.");
        }
        // non-static block
        {
                System.out.println("father non-static block init.");
        }

        // constructor
        public FatherBall() {
                System.out.println("father constructor execute.");
        }

        private static int fxInit() {
                System.out.println("father static field init.");
                return 2;
        }

        private int faInit() {
                System.out.println("father non-static field init.");
                return 2;
        }
}

SelfBall.java

/**
 * self ball,test class init & object creation
 * @author eric
 * @date 2010-3-19  Morning  11:44:19
 */
public class SelfBall extends FatherBall {
        // static fields
        protected static int sx = sxInit();
        protected static int sy;
        // non-static fields
        protected int sa = saInit();
        // static block
        static {
                System.out.println("self static block init.");
        }
        // non-static block
        {
                System.out.println("self non-static block init.");
        }

        // constructor
        public SelfBall() {
                System.out.println("self constructor execute.");
        }

        private static int sxInit() {
                System.out.println("self static field init.");
                return 3;
        }

        private int saInit() {
                System.out.println("self non-static field init.");
                return 3;
        }

        // main
        public static void main(String[] args) {
                System.out.println();
                System.out.println("executeing main() ... class has been loaded.");
                System.out.println("object is going to be created:");
                System.out.println();
                // create object
                SelfBall sb1 = new SelfBall();
                System.out.println();
                System.out.println("object created.");
        }
}
分类:Java 时间:2010-03-19 人气:217
分享到:
blog comments powered by Disqus

相关文章

  • Summary of the Effective Java (1 )---- object creation and destruction 2010-07-29

    Summarizes recent re-entry under the Effective Java, be it under review 1. Object creation and destruction 1.1 consider the use of static factory methods instead of constructors Benefits: And construction methods, the static method name, when there a

  • Java: object creation and initialization process 2010-03-20

    Analysis of Java object creation and initialization process-related conceptual issues involved, java in the stack (stack) and heap (heap), objects, references, handle concept. 1. Java Data Types Java, there are three data types: basic data types (in

  • The efficiency of object creation 2010-05-09

    First to introduce the next object created in several ways: 1. New ways to create var stdClass = function(){}; var obj = new stdClass(); 2. Plain object creation method var obj = {}; 3. New Object creation method var obj = new Object(); So these thre

  • Reduce the object creation to improve java performance 2010-06-02

    Many common Java performance problems early in the design process originated in the kind of design thinking, as early as many developers began to consider the issue before the performance. In this series, Brian Goetz discusses Java performance on the

  • java based - object-oriented programming, method overloading, object creation and use of 2010-10-04

    Object-oriented programming: Content: 1. The development of programming languages 2. For the design process 3. Object-oriented design 4. The concept of objects and classes 5. The relationship between classes 6. The object and reference 7. JAVA class

  • RUBY Design Model Builder Model Builder (simplified object creation) 2010-03-18

    Builder class is responsible for all the components bottled in a complex object. Construction of a complex object from its representation separation, so that the same construction process can create different representations. 14.2 UML class diagram g

  • extjs panel Objects prompt window view area shows object creation 2010-05-16

    The page's code line by line first.jsp <%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <% request.setAttribute("base", request.getContextPath()); %> <!DOCTYPE

  • extjs panel Objects prompt window view area shows object creation (1) 2010-05-16

    The page's code line by line first.jsp <%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <% request.setAttribute("base", request.getContextPath()); %> <!DOCTYPE

  • Java Object loaded into memory in order 2010-04-21

    class A extends java.lang.Object ( . . . ) class B extends A ( public static void main (String args []) ( ClassA ca = new ClassA (); ) Object loaded into memory 1. A class loaded into memory, the implementation of A class static block 2. B class load

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.338 (s). 10 q(s)