Topic: 一个类中的一段static代码在什么时候执行?

  Print this page

1.一个类中的一段static代码在什么时候执行? Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 13:20

例如:

public abstract class ActionFactory
{
static ActionFactory defaultActionFactory;
static ActionFactory actionFactoryImplementation;

static //我指的是这段static代码
{
// Create default implementation
try
{
String className = Configuration.getString("webwork.action.factory");
try
{
defaultActionFactory = (ActionFactory)Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
}
catch (Exception e)
{
LogFactory.getLog(ActionFactory.class).error("Could not instantiate action factory:" + e);
defaultActionFactory = new DefaultActionFactory();
}
}
catch (IllegalArgumentException ex)
{
LogFactory.getLog(ActionFactory.class).error("Could not instantiate configuration:" + ex);
defaultActionFactory = new DefaultActionFactory();
}
}

/**
* Returns the matching action found as the result of traversing the action factory delegation chain.
* @param aName name of action to return
* @return action located through the factory delegation chain
*/
public static Action getAction(String aName) throws Exception
{
return getActionFactory().getActionImpl(aName);
}

/**
* Returns the action factory implementation or the default action factory if not available.
* @return action factory implementation
*/
public static ActionFactory getActionFactory()
{
return actionFactoryImplementation == null ? defaultActionFactory : actionFactoryImplementation;
}

/** Set the action factory implementation. Can only be called once. */
public static void setActionFactory(ActionFactory aFactory) throws IllegalStateException
{
// new Throwable().printStackTrace();
if (actionFactoryImplementation != null)
throw new IllegalStateException("May only set action factory implementation once");
actionFactoryImplementation = aFactory;
}

/**
* Returns the action object for the specified action or a matching action on the action factory delegation chain.
* @param aName name of action to check for a match
*/
public abstract Action getActionImpl(String aName) throws Exception;
}


其中的static代码段是在实例化它的具体子类时执行的吗?

2.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: scottding
Posted on: 2003-01-09 13:23

当这个类加载的时候就开始了,注意,这个时候还没有构造,是在构造之前。

3.Re:一个类中的一段static代码在什么时候执行? [Re: scottding] Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 13:54

scottding wrote:
当这个类加载的时候就开始了,注意,这个时候还没有构造,是在构造之前。


这个类加载的时候是什么时候?能否具体举个例子?

4.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2003-01-09 14:31

在你第一次使用这个类的static元素或第一次构造对象时,
先调用static代码。
按我的理解就是作任何有关这个类的事时都先调用static代码。

5.Re:一个类中的一段static代码在什么时候执行? [Re: mitnickcbc] Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 15:29

mitnickcbc wrote:
在你第一次使用这个类的static元素或第一次构造对象时,
先调用static代码。
按我的理解就是作任何有关这个类的事时都先调用static代码。


这段static代码只运行一次,就是最先用这个类时执行。对吗?
是不是第一次用到这个类时执行之后,再有子类实例化时就不会再执行这段代码了?

6.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2003-01-09 15:43

yadan wrote:
这段static代码只运行一次,就是最先用这个类时执行。对吗?
是不是第一次用到这个类时执行之后,再有子类实例化时就不会再执行这段代码了?



7.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 15:58

谢谢楼上两位!

8.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: scottding
Posted on: 2003-01-09 16:30

hoho,发现他头像下面多了一行字哦,Jute User,还有一个金杯哦。羡慕中~~~~~~~~~~~~~~

9.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 22:40


hoho,发现他头像下面多了一行字哦,Jute User,还有一个金杯哦。羡慕中~~~~~~~~~~~~~~

哈哈 是很有意思SmileSmile

10.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: snowbug
Posted on: 2003-01-09 23:02

The order of the class initialization:
1. static variables and static blocks, based on the order they appear in the class (this only happens at the first time the class is accessed)
2. the non-static variables
3. constructor

The above steps (1 through 3) are repeated top-down in the class inheritance tree. For example, if class A is the sub-class of B and B is the sub-class of the root class Object, then the following initialization will occur when you call A a = new A();:
i) do step 1 through 3 for the root class Object
ii) do step 1 through 3 for class B
iii) do step 1 through 3 for class A

11.Re:一个类中的一段static代码在什么时候执行? [Re: snowbug] Copy to clipboard
Posted by: yadan
Posted on: 2003-01-09 23:05

snowbug wrote:
The order of the class initialization:
1. static variables and static blocks, based on the order they appear in the class (this only happens at the first time the class is accessed)
2. the non-static variables
3. constructor

The above steps (1 through 3) are repeated top-down in the class inheritance tree. For example, if class A is the sub-class of B and B is the sub-class of the root class Object, then the following initialization will occur when you call A a = new A();:
i) do step 1 through 3 for the root class Object
ii) do step 1 through 3 for class B
iii) do step 1 through 3 for class A


这么一说完全清楚了!thks!

12.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: cypu
Posted on: 2003-01-20 10:45

相当于全局函数,只执行一次!
可以用Class.forName(String)执行

13.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: thomasc
Posted on: 2003-01-20 14:24

最先调用的。cypu说的对

14.Re:一个类中的一段static代码在什么时候执行? [Re: snowbug] Copy to clipboard
Posted by: Breeze
Posted on: 2003-01-22 22:51

snowbug wrote:
The order of the class initialization:
1. static variables and static blocks, based on the order they appear in the class (this only happens at the first time the class is accessed)
2. the non-static variables
3. constructor

The above steps (1 through 3) are repeated top-down in the class inheritance tree. For example, if class A is the sub-class of B and B is the sub-class of the root class Object, then the following initialization will occur when you call A a = new A();:
i) do step 1 through 3 for the root class Object
ii) do step 1 through 3 for class B
iii) do step 1 through 3 for class A


这个说法应该是错误的。
具体应该是这样的:
1、root->B->A,执行step 1.
2、root执行step 2,step3
3、B执行step 2,step 3
4、A执行step 2,step 3.

15.Re:一个类中的一段static代码在什么时候执行? [Re: yadan] Copy to clipboard
Posted by: floater
Posted on: 2003-01-24 05:47

Ok, let's run some test:

Here is the results:


In StaticB static block - 1 - run only once
sb=static variable sb
In StaticA static block - 2 - run only once
sa=static variable sa
In StaticB constructor - 3
In StaticA constructor - 4
In StaticB constructor - 3
In StaticA constructor - 4


The code is at the end. We can deduct from these:
static blocks are executed only once, when their classes are reference.
Both parent and child static blocks run before constructors.

Note: If you decompile the class files, you would find that the non-static variable initializations are moved into constructors, like nsa and nsb are moved in the code below.


package statictest;

public class StaticA extends StaticB
{
  public String nsa = "non-static variable nsa";
  static public String sa = "static variable sa";

  static
  {
    System.out.println("In StaticA static block - 2 - run only once");
    System.out.println("sa=" + sa);
  }

  public StaticA()
  {
    super();
    System.out.println("In StaticA constructor - 4");
  }
}



package statictest;

public class StaticB
{
  public String nsb = "non-static variable nsb";
  static public String sb = "static variable sb";

  static
  {
    System.out.println("In StaticB static block - 1 - run only once ");
    System.out.println("sb=" + sb);
  }

  public StaticB()
  {
    super();
    System.out.println("In StaticB constructor - 3");
  }
}




package statictest;

public class StaticTest
{
  public static void main(String[] args)
{
    StaticA sa = new StaticA();
    StaticA sa1 = new StaticA();
  }
}


16.Re:一个类中的一段static代码在什么时候执行? [Re: Breeze] Copy to clipboard
Posted by: snowbug
Posted on: 2003-01-25 12:26

Breeze wrote:
这个说法应该是错误的。
具体应该是这样的:
1、root->B->A,执行step 1.
2、root执行step 2,step3
3、B执行step 2,step 3
4、A执行step 2,step 3.


谢谢指出错误 Thumbs up


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923