简体   繁体   English

如何在同一 class 中的 java 中访问存储在另一种方法中的变量

[英]How to access a variable stored in another method in java in the same class

I need to access the variable "value" in the second method.我需要在第二种方法中访问变量“值”。 I tried making it an instance variable but it just returned as a null.我尝试将其设为实例变量,但它只是作为 null 返回。

public class RateFellowUnsatisfactoryPage extends PageObject {

@FindBy(xpath = "actual xpath")
private WebElementFacade checkbox;

@FindBy(xpath = "actual xpath")
private WebElementFacade fellowRated;   

private String value;

public void selects_first_checkbox() {
    value = checkbox.getValue();
    System.out.println(value);
    checkbox.click();
}

public void verifyFellowDisplayed() {
    String fellow = fellowRated.getTextValue();
    System.out.println(fellow);
    assertThat(fellow.equalsIgnoreCase(value));
    System.out.println(value);
}
}

What you're doing looks correct.你在做什么看起来是正确的。 Are you sure that selects_first_checkbox has been called already at the point that you're calling verifyFellowDisplayed for this instance of RateFellowUnsatisfactoryPage?您确定在您为此 RateFellowUnsatisfactoryPage 实例调用 verifyFellowDisplayed 时已经调用了 selects_first_checkbox 吗? Obviously, if it has not yet been set, it will still be null.显然,如果还没有设置的话,还是null。

Assuming it has been called, is it being called in the same thread?假设它已被调用,它是否在同一个线程中被调用? Java will sometimes optimize away references to member variables. Java 有时会优化掉对成员变量的引用。 Try declaring it volatile, which should fix it as long as you're using (I think) Java 8 or higher.尝试将其声明为 volatile,只要您使用(我认为)Java 8 或更高版本,它就应该修复它。 If that doesn't fix it, try declaring it to be AtomicReference and you'll have an extra step to access it.如果这不能解决问题,请尝试将其声明为 AtomicReference,您将有一个额外的步骤来访问它。 Sorry, but I forget exactly what.对不起,但我忘了具体是什么。 The point of AtomicReference is that it will not optimize away any reference to it. AtomicReference 的要点是它不会优化对它的任何引用。

暂无
暂无

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

相关问题 如何从“ onCreate()”访问同一类的另一个方法中的Arraylist变量? - How to access from “onCreate()” an Arraylist variable in another method of same class? 如何访问在另一个Java类的方法中声明的变量? - How to access a variable which is declared in a method belonging to another Java class? 如何使用方法内部定义的变量在Java中同一类的另一个方法中的方法外部使用 - How to use a variable defined inside a method use outside the method in another method on the same Class in java 如何访问另一个 class 的方法变量? - How to access a variable of method of an another class? 如何访问另一个方法中定义的变量(在Java中)? - How to access a variable defined in another method (In java)? java - 如何访问另一个文件中另一个类的方法,该文件在java中的同一个包中? - How to access a method of another class in another file which is in same package in java? java中如何在Main方法中访问同名变量不同类变量 - How to Access same name Variable Different Class Variable in Main Method in java 如何在Java中同一包中的不同文件上从一个类访问另一个类的变量 - How to access a variable from one class into another class on different files in same package in java java中其他class的方法如何访问变量 - How to access the variable in the method of other class in java 如何在同一个类(Java)中的另一个方法中引用一个方法中的变量? - How can I reference a variable within a method in another method in the same class (Java)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM