我可以访问在另一个类的静态方法中声明的静态变量吗

时间:2018-08-31 00:21:55

标签: java

我有一个静态变量,它存在于静态方法中。我可以从另一个班级访问它吗?

PFB我的代码:CellValue是我需要在另一个类(下面的代码2)中访问的变量。

代码1:

public static String[][] getSheet(String dataSheetName) {
    String[][] data = null;
    try {
        FileInputStream fis = new FileInputStream(new File("./data/"+dataSheetName+".xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);   

        // get the number of rows
        int rowCount = sheet.getLastRowNum();

        // get the number of columns
        int columnCount = sheet.getRow(0).getLastCellNum();
        data = new String[rowCount][columnCount];


        // loop through the rows
        for(int i=1; i <rowCount+1; i++){
            try {
                XSSFRow row = sheet.getRow(i);
                for(int j=0; j <columnCount; j++){ // loop through the columns
                    try {
                        String cellValue = "";
                        try{
                            Cell cell1 = sheet.getRow(i).getCell(0);   

                            switch (cell1.getCellType()) {
                            case Cell.CELL_TYPE_STRING:
                                cellValue = cell1.getStringCellValue();
                                break;
                            }
                            cellValue = row.getCell(j).getStringCellValue();
                        }catch(NullPointerException e) {

                        }

代码2:

public HomePage engagementDetails(String why, String who, String block, String streetname, String unit, String postalcode) {
        System.out.println("Not able to click");
        DataInputProvider dp = DataInputProvider.getSheet();
        if(getSheet.cellValue == "1") {
        clickbyXpath(prop.getProperty("AddNewForm.A9Yes_Xpath"));
        }
        else if (DataInputProvider.cellValue == "2") {
        clickbyXpath(prop.getProperty("AddNewForm.A9Yes_Xpath"));
        enterbyId(prop.getProperty("AddNewForm.A9Why_Id"), why);
        }
        clickbyXpath(prop.getProperty("AddNewForm.A10Yes_Xpath"));
        enterbyId(prop.getProperty("AddNewForm.A10Who_Id"), who);
        clickbyXpath(prop.getProperty("AddNewForm.A11No_Xpath"));
        enterbyId(prop.getProperty("AddNewForm.A11Block_Id"), block);
        enterbyId(prop.getProperty("AddNewForm.A11StreetName_Id"), streetname);
        enterbyId(prop.getProperty("AddNewForm.A11Unit_Id"), unit);
        enterbyId(prop.getProperty("AddNewForm.A11PostalCode_Id"), postalcode);
        clickbyXpath(prop.getProperty("AddNewForm.A12OTH_Xpath"));
        clickbyXpath(prop.getProperty("AddNewForm.A13Lang_Xpath"));
        clickbyXpath(prop.getProperty("AddNewForm.A13Photo_Xpath"));
        scrollPage();
        clickByName(prop.getProperty("AddNewForm.Next_Name"));
        return this;
    }

谢谢。

1 个答案:

答案 0 :(得分:2)

否,局部变量的可见性受限于声明它们的代码块,在这种情况下,它是第二个try循环内的for块。

可以声明一个静态类变量并获得对该变量的访问权限,即

static String cellValue = "";

,然后您可以为cellValue分配一个不同的字符串(请注意Java字符串是不可变的)。

但是,依靠(静态)类变量和类方法被视为代码异味:这暗示着类设计不良。如果这些类变量更改了值(更改程序的全局状态),则认为这是代码的味道。

最好这样做:

  1. 重构代码并使方法作用于类实例(即不使用static);
  2. 将业务逻辑和UI层分开。
相关问题