简体   繁体   English

将一个对象从一个类转到另一个类?

[英]Gettin an object from one class to another?

Need to bring the information from one class to the other, I need the final value of fin to return in getResultado.需要将信息从一个类带到另一个类,我需要在 getResultado 中返回 fin 的最终值。

public class Setter {
 String fin = "";
public  Setter(String result){
fin = result;
}
public String getResultado(String inicio){
return fin;
}
}

Here is the other class where I need to implement fin:这是我需要实现 fin 的另一个类:

public Final(){
Setter t = getResultado();
    System.out.println(t);

Problem is very simple: You never initialize an instance of your Setter class.问题很简单:你永远不会初始化你的Setter类的实例。 You create a reference/pointer to a Setter object, but its null and you are trying to call a method contained in this class.您创建了一个指向Setter对象的引用/指针,但它为null并且您正在尝试调用此类中包含的方法。 So, you should probably be getting a NullPointerException .所以,你可能应该得到一个NullPointerException The fix to this is simple, just解决这个问题很简单,只需

initialize the object:初始化对象:

Setter t = new Setter("my string");

then call the method through your instance of Setter:然后通过您的 Setter 实例调用该方法:

t.getResultado();

Edit: If you don't want to give the variable fin a value when you create an object Setter , just add a second constructor right underneath the first one like this:编辑:如果您不想在创建对象Setter时给变量fin一个值,只需在第一个构造函数下方添加第二个构造函数,如下所示:

public Setter() {
   //do whatever, maybe call the getResultado() method right here?
}

It will allow you to create Setter objects without any parameters or changing value of the variable.它将允许您创建没有任何参数或更改变量值的 Setter 对象。

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

相关问题 如何将值从一个类对象保存到另一类对象 - how to save values from one class object to another class object 如何将对象从一类转移到另一类-JAVA - How to transfer object from one class to another class - JAVA 将大多数属性从一个 class object 复制到另一个 class - Copy most attributes from one class object to another class 如何将对象从一个类转到另一个类? - How to get an object from one class to another class? 如何将 object 从一个 class 存储到另一个 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 的 arraylist 中 - How to I store an object in an arraylist from one class in another class 将数据从一个对象从一个类复制到Android中另一个类的另一个项目 - Copy data from one object from one class to another boject in another class in Android 有没有办法将一个对象从一个类复制到另一个完全不同的类中的对象? - Is there a way to copy one object from one class into another object from an entirely different class? 从一个对象类自动转换到另一个对象类 - Automatic casting from one object-class to another 在按钮单击时将对象从一个类传递到另一个类 - Passing object from one class to another on a Button click 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM