源码出自著名的开源软件nutch。如下:
public class BasicUrlNormalizer implements UrlNormalizer 
{
    ……
    //建立了两个inner static class实例
    private Rule relativePathRule = null;
    private Rule leadingRelativePathRule = null;
    
    //外部类的构造函数,在其中对两个实例都进行了操作
    public BasicUrlNormalizer() 
    {
        relativePathRule = new Rule();
        relativePathRule.pattern = (Perl5Pattern)
                      compiler.compile("(/[^/]*[^/.]{1}[^/]*/\\.\\./)",
                      Perl5Compiler.READ_ONLY_MASK);
        relativePathRule.substitution = new Perl5Substitution("/");
        leadingRelativePathRule = new Rule();
        leadingRelativePathRule.pattern = (Perl5Pattern)
          compiler.compile("^(/\\.\\./)+", Perl5Compiler.READ_ONLY_MASK);
        leadingRelativePathRule.substitution = new Perl5Substitution("/");
    } 
    catch (MalformedPatternException e) 
   {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
        throw new RuntimeException(e);
   }
 }
    ……
    //定义的inner static 类Rule
    private static class Rule 
    {
        public Perl5Pattern pattern;
        public Perl5Substitution substitution;
    }
本人是java菜鸟,请各位大虾指点:
(1)Rule没有构造函数,怎么实例化?
(2)static类的member只有一份copy,怎么两个实例同时操作?

解决方案 »

  1.   

    构造函数开头掉了个trypublic BasicUrlNormalizer() 
    try{
            relativePathRule = new Rule();
        ……
      

  2.   

    1,默认构造函数
    2,
    成员类(member class):成员类作为一个class的一部分,你可以在class中的任何地方使用成员类,当你想用成员类的变量和方法你可以不需要外在的授权成员类是唯一的一个你可以声明为static的类,当你声明成员的时间,你仅仅在成员类的所属类中可以引用,如果你想去掉这个约束,你可以声明一个成员类为static类。当你声明一个static成员类,他就变成了嵌套的外部类,并且可以象正常类一样http://dev.cbw.com/java/sunjava/20055245411_3257871.shtml也就是说声明为public static class SomeClass,则这个类可以跟一般的public类一样使用,只不过类名在使用的时候要OutClass.SomeClass kls = new OutClass.SomeClass(...);
      

  3.   

    谢谢楼上的回复,也就是说这里的static只是让外部能调用这个类,而没有“公用”的意思?
    我看代码也是这样的感觉。
      

  4.   

    我觉得对于内部类来说,private static跟private是一样的,static让内部类跟一般的类一样可以被其他类所使用(而不仅仅被它的外部类使用),但是一旦声明为private,则又把访问权限给限制了,即除了外部类,其他类仍然无法访问,就跟一般的private的内部类一样了