从另一个类调用变量时获取到空值。

huangapple 未分类评论45阅读模式
英文:

Getting Null when calling variable from another class

问题

我已经对此进行了大量的Google搜索,并尝试了许多方法,但从未成功。所以我有一个在Class A中的变量,并且我想在Class B中打印该变量,但在调用该变量之前,Class A中的变量的值会经过一个方法。

public class TestSubject extends javax.swing.JFrame {

static String a; 

private void testButtonActionPerformed(java.awt.event.ActionEvent evt) { 
   a = "hello"; }
}

在这里,我声明了变量并点击按钮将变量的值设置为'hello'。我通过在同一类中有另一个按钮来打印变量'a'来测试变量是否确实获得了值。

public class TestSubject2 extends javax.swing.JFrame {

TestSubject test = new TestSubject(); 

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                    
        System.out.println(test.a);
    }                  
}

所以在这里,我尝试从类'TestSubject'中打印变量'a'。

所以我尝试过:

我尝试使用非静态和静态两种方式。

我尝试使用返回方法

class TestSubject {   //故意留下了扩展,但你知道我的意思
public String getString() {
   return a; } }
Class TestSubject2 {
   TestSubject test = new TestSubject(); 

   private void okActionPerformed(java.awt.event.ActionEvent evt) {                                    
        System.out.println(test.getString());
    }                 
}

然而,如果我明确为'a'赋一个值,比如:

static String a = "hello"; 

它将使用第一种方法在另一个类中正确打印出来。

我确保按顺序点击按钮以输入值。

所以我想知道的是如何从另一个类中调用变量。

英文:

I have done a lot of google regarding this and tried many things and have never gotten through. So I have this variable in Class A and I want to print that variable in Class B but the value of the variable in Class A goes through a method before it is called.

public class TestSubject extends javax.swing.JFrame {

static String a; 

private void testButtonActionPerformed(java.awt.event.ActionEvent evt) { 
   a = "hello"; }
}

Here I declare the variable and clicks a button to set the variable value to 'hello'. I tested if the variable did indeed get the value by having another button printing the variable 'a' in the same class.

public class TestSubject2 extends javax.swing.JFrame {

TestSubject test = new TestSubject(); 

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                    
        System.out.println(test.a);
    }                  
}

So here I try print the variable a from class 'TestSubject'.

So what I have tried:

I have tried to use both non-static and static.

I have tried to use a return method

class TestSubject {   //purposely left the extend but you get what I mean
public String getString() {
   return a; } }
Class TestSubject2 {
   TestSubject test = new TestSubject(); 

   private void okActionPerformed(java.awt.event.ActionEvent evt) {                                    
        System.out.println(test.getString());
    }                 
}

However, if I were to explicitly give 'a' a value like:

static String a = "hello"; 

It would print out properly in the other class using the first method.

I made sure I click the buttons sequentially so the values are being inputted.

So what I want to know is how can I call a variable from another class.

huangapple
  • 本文由 发表于 2020年4月7日 08:54:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61071173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定