문제

I have this rather simple code written in java. This is actually from a DAQ framework, called Kmax

import kmax.ext.*; 

public class Runtime implements KmaxRuntime {
    KmaxToolsheet tlsh; // Store a reference to the toolsheet environment
    KmaxHist hist1D;
    KmaxWidget checkBoxWidget;

    public void init(KmaxToolsheet toolsheet) {
        tlsh = toolsheet; // Save this reference for use in the toolsheet
        hist1D = tlsh.getKmaxHist("HIST1D");
        checkBoxWidget = tlsh.getKmaxWidget("CHECK_BOX_CALIB_METH");
        tlsh.getKmaxWidget("CHECK_BOX_CALIB_METH").setProperty("VALUE", "1");

    }

    public void CalibInit(KmaxWidget widget, KmaxHist histo){
        histo.setUseXAxisCalibration(stringToBool(widget.getProperty("VALUE")));
        histo.update();

    }
    CalibInit(checkboxWidget,hist1D);

    public void GO(KmaxToolsheet toolsheet){}
    public void SRQ(KmaxDevice device) {}
    public void HALT(KmaxToolsheet toolsheet) {}

} // End of the Runtime object

Note that there I have created an object named CHECK_BOX_CALIB_METH. When I compile this code I get those errors messages

compiler msg>error: invalid method declaration; return type required
compiler msg>   CalibInit(checkboxWidget,hist1D);
compiler msg>   ^

compiler msg>error: <identifier> expected
compiler msg>CalibInit(checkboxWidget,hist1D);
compiler msg>                        ^


compiler msg>error: <identifier> expected
compiler msg>CalibInit(checkboxWidget,hist1D);
compiler msg>                               ^

Note that if I remove the CalibInit method and replace it with

public void CHECK_BOX_CALIB_METH(KmaxWidget widget) {

    hist1D.setUseXAxisCalibration(stringToBool(widget.getProperty("VALUE")));
    hist1D.update();

}

I get no compile error. The keypoint is that the method's name is the same as the object's name. The reason I created CalibInit() is to avoid having each method for every object of the same type, with the same functionality. Is there a way around it?

How to avoid those errors?

도움이 되었습니까?

해결책 2

The code

CalibInit(checkboxWidget,hist1D); 

that is on a line of its own is not inside any of your methods. The compiler assumes that this is a new method declaration which is probably not what you want.

Side note:
It is not recommended to have methods starting with a upper case character: "Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized." from Code Conventions for the Java Programming Language

다른 팁

Only variables can declare out side of methods. You can call methods only in methods and constructor (avoiding static context here).

  CalibInit(checkboxWidget,hist1D);

Please move that line to any method or constructor , if needed. More specifically call where you need it.

In short: CalibInit(checkboxWidget,hist1D); is orphan now. make it belong to something.

You can't call

CalibInit(checkboxWidget,hist1D);

directly in the class like you're doing. This instruction should be inside a constructor if your goal is to call it when an instance of Runtime is constructed.

BTW: methods start with a lowercase letter by convention in Java, and you shouldn't call your class Runtime: it will confuse people because a standard Runtime class already existsin the standard libraries.

You are calling CalibInit(checkboxWidget,hist1D) method directly in the class not in any method. Java doesn't support this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top