Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>To Answer Your Original Question</h2> <p>Long story short, you can't access method variables externally. What you <em>want</em> to do is make those variables <strong>fields</strong> within the class. Putting them outside the method means they stick around after the method is done, and it means you can access them from outside.</p> <pre><code>public class EnterLeaveHandler implements IOtfHandler { private long time; private int func; private int cpuid; private int source; // Please don't use varargs like this; read the whole answer!! public void handle(Object ... args) { time = (Long) args[0]; func = (Integer) args[1]; cpuid = (Integer) args[2]; source = (Integer) args[3]; } } </code></pre> <p>Then you access them by creating <strong>getters</strong> and <strong>setters</strong>:</p> <pre><code>public long getTime() { return time; } public void setTime(long t) { time = t; } // etc... </code></pre> <hr> <h2>HOWEVER, Some Suggestions...</h2> <p>Your code is... strange, to say the least. It's also very non-Java-like. As much as possible, you should try to avoid having multiple overriding methods that need different data. Also, you normally want to initialize your <em>fields</em> in the <strong>constructor</strong>, not in some other method.</p> <p>It's not clear how much of the code you have access to, but if you're able to rewrite the interface, I would <strong>definitely</strong> do so. <code>Object</code> varargs in an interface is just weird. The reason for using an interface is so that you can call an interface method with identical parameters and, regardless of the object type underneath, something useful will happen. It defeats the point of the interface to have two implementations of the same method require totally different arguments. The following code demonstrates why this is:</p> <pre><code>IOtfHandler h1 = new EnterLeaveHandler(); IOtfHandler h2 = new DefFunctionHandler(); h1.handle(0, 0, 0, 0); h2.handle(0, 0, 0, 0); // Crashes with ClassCastException!! :( // And would also crash two lines later with ArrayIndexOutOfBoundsException </code></pre> <p><strong>Much better</strong> to just make them different methods entirely.You know what variables you're expecting, so you should take advantage of that fact. Your method signatures would be far better off looking something like this:</p> <pre><code>public class EnterLeaveHandler implements IOtfHandler { public void handle(long time, int func, int cpuid, int source) { // Do things with your shiny new variables } public class DefFunctionHandler implements IOtfHandler { public void handle(int stream, int func, String name, int funcgroup, int source) { // Do things with your shiny new variables } } </code></pre> <p>As others have suggested, if the "real" method signatures are not identical, you shouldn't be using an interface. Better to use an abstract base class instead, to hold what little data is common between them:</p> <pre><code>abstract class IOtfHandler { private int source; private int func; public void setSource(int source) { this.source = source; } // etc } class EnterLeaverHandler extends IOtfHandler { private long time; // etc } class DefFunctionHandler extends IOtfHandler { private String name; // etc } </code></pre> <p>Of course, if you set all the variables in the constructors, you may be able to add an abstract <code>handle()</code> method to the base class, since then that method should have the same signature, and take no arguments at all!</p> <hr> <h2>Final Result</h2> <p>So if we pull together all the changes I've talked about-- moving the method variables into <em>fields</em>, using <em>getters</em> and <em>setters</em>, using <em>useful method signatures</em>, using <em>constructors</em>, and using a <em>base class</em> instead of a misleading interface, we end up with something like this:</p> <pre><code>abstract class IOtfHandler { private int source; private int func; public void setSource(int source) { this.source = source; } public int getSource() { return source; } public void setFunc(int func) { this.func = func; } public int getFunc() { return func; } // abstract handle method abstract public void handle(); } class EnterLeaverHandler extends IOtfHandler { private long time; private int cpuid; // getters and setters public void setTime(long time) { this.time = time; } public long getTime() { return time; } public void setCpuId(int cpuid) { this.cpuid = cpuid; } public int getCpuId() { return cpuid; } // constructor public EnterLeaverHandler(long time, int cpuid, int source, int func) { setTime(time); setCpuId(cpuid); setSource(source); setFunc(func); } // handle method public void handle() { System.out.println("EnterLeaverHandler.handle()"); // Do whatever class-specific handling you might want to do in here. } } class DefFunctionHandler extends IOtfHandler { private String name; private int funcGroup; private int stream; // getters and setters public void setName(String name) { this.name = name; } public String getName() { return name; } public void setFuncGroup(int funcGroup) { this.funcGroup = funcGroup; } public int getFuncGroup() { return funcGroup; } public void setStream(int stream) { this.stream = stream; } public int getStream() { return stream; } // constructor public DefFunctionHandler(String name, int funcGroup, int stream, int source, int func) { setName(name); setFuncGroup(funcGroup); setStream(stream); setSource(source); setFunc(func); } // handle method public void handle() { System.out.println("DefFunctionHandler.handle()"); // Do whatever class-specific handling you might want to do in here. } } public class Main { public static void main(String[] args) { IOtfHandler h1 = new DefFunctionHandler("name", 0, 0, 0, 0); IOtfHandler h2 = new EnterLeaverHandler(0, 0, 0, 0); h1.handle(); h2.handle(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload