开发者

Instantiating/Referencing static nested class inside static nested class

开发者 https://www.devze.com 2023-03-07 05:59 出处:网络
I have been give a jar file to use that has a static inner class inside of another static inner class:

I have been give a jar file to use that has a static inner class inside of another static inner class:

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. Whenever I add:

import externalJarFile.Job.GlobalVars.Variable;

I get a compilation error that the type "externalJarFile.Job.GlobalVars.Variable" cannot be referenced. How can I create a new开发者_StackOverflow "Variable" instance to add to the "GlobalVars.getVariable()" list?

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. 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. 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:

    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.

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

精彩评论

暂无评论...
验证码 换一张
取 消