简体   繁体   English

在静态嵌套类中实例化/引用静态嵌套类

[英]Instantiating/Referencing static nested class inside static nested class

I have been give a jar file to use that has a static inner class inside of another static inner class: 我已经给了一个jar文件,它在另一个静态内部类中有一个静态内部类:

package externalJarFile;

public class Job
{
  public static class GlobalVars
  {
    private List<Job.GlobalVars.Variable> variables;
    public List<Job.GlobalVars.Variable> getVariable()
    {
        if (this.variables == null) {
            this.variables = new ArrayList<Job.GlobalVars.Variable>();
        }
        return this.variables;
    }

    public static class Variable
    {
      String name;
      String value;
      public String getName() { return name; }
      public void setName( String name ) { this.name = name; }
      public String getValue() { return value; }
      public void setValue( String value) { this.value= value; }
    }
  }  
}

The problem I'm having is that I need to populate the "Job.GlobalVars" list, but I can't figure out how to reference the "Variables" type. 我遇到的问题是我需要填充“Job.GlobalVars”列表,但我无法弄清楚如何引用“变量”类型。 Whenever I add: 每当我添加:

import externalJarFile.Job.GlobalVars.Variable;

I get a compilation error that the type "externalJarFile.Job.GlobalVars.Variable" cannot be referenced. 我得到一个编译错误,无法引用类型“externalJarFile.Job.GlobalVars.Variable”。 How can I create a new "Variable" instance to add to the "GlobalVars.getVariable()" list? 如何创建新的“变量”实例以添加到“GlobalVars.getVariable()”列表?

Here's a snippet that I tried (but didn't work): 这是我尝试过的片段(但没有用):

Job.GlobalVars vars = new Job.GlobalVars();
Job.GlobalVars.Variable v = new Job.GlobalVars.Variable();

[ Edited for clarity ] [ 编辑清晰 ]

[ UPDATE ] [ 更新 ]

Ok, this is kinda weird. 好的,这有点奇怪。 If I take the code from the original project and directly import it into mine, I'm able to reference the inner-inner-class. 如果我从原始项目中获取代码并直接将其导入到我的代码中,我就可以引用内部内部类。 However, when I reference it when it's packaged inside of a jar file, it fails. 但是,当我在jar文件中打包时引用它时,它会失败。 MTK... MTK ...

You forgot a space: 你忘了一个空间:

Job.GlobalVars vars = new Job.GlobalVars();
               ^

This works fine for me: 这对我来说很好:

Job.GlobalVars.Variable var = new Job.GlobalVars.Variable();
var.setName("MByD");

Class job: 上课工作:

package mypackage;

import java.util.ArrayList;
import java.util.List;

public class Job {
      public static class GlobalVars
      {
        private List<Variable> variables;
        public List<Variable> getVariable()
        {
            if (this.variables == null) {
                this.variables = new ArrayList<Variable>();
            }
            return this.variables;
        }

        public static class Variable
        {
          String name;
          String value;
          public String getName() { return name; }
          public void setName( String name ) { this.name = name; }
          public String getValue() { return value; }
          public void setValue( String value) { this.value= value; }
        }
      }  
}

Other class using GlobalVars and Variable. 使用GlobalVars和Variable的其他类。 Import works very good. 进口效果非常好。

package mypackage;

import mypackage.Job.GlobalVars;
import mypackage.Job.GlobalVars.Variable;

public class RunIt {
    public static void main(String[] args) {
        GlobalVars vars = new GlobalVars();
        Variable v = new Variable();
    }
}

No need to import anything. 无需进口任何东西。 You should be able to just refer to your inner class by its name, "Variable", from the "Job" class: 您应该能够通过“Job”类中的名称“Variable”来引用您的内部类:

    private List<Variable> variables;

    public List<Variable> getVariable()

They way you had stated above is correct. 你上面说的方式是正确的。 You should check to ensure that the jar file is in your classpath as that would definitely cause the import to fail and subsequently all future declarations. 您应该检查以确保jar文件位于类路径中,因为这肯定会导致导入失败以及随后的所有声明。

import mypackage.Job.GlobalVars.Variable;
...
Variable v = new Variable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM