简体   繁体   中英

How to pass Object from java class to another java class

I created an instance of a class in java as following:

ABC ab = new ABC();

I want to access this instant ab in another class XYZ . How to make this Object available in class XYZ?

It is difficult to answer your question without more specific information about your problem, but this would certainly work:

You can use a setter to initialize an instance variable in your other class if you want to use it everywhere in that class:

class someClass {
   void someMethod() {
      ABC ab = new ABC();
      XYZ xyz = new XYZ();
      xyz.setABC(ab);
   }
}

class XYZ {
   ABC ab;
   //...
   void setABC(ABC ab) {
     this.ab = ab;
   }
   //...
   void doSomething() {
      // do something with instance variable ab
   } 
}

There are several ways to do what you want to achieve. Some of them might be as follows:

Passing the object reference through a constructor
Here, you would explicitly pass the reference of your reference class when you're creating an object of the actual class.

public class ActualClass{
    private ReferenceClass refClassObject;

    /**
    * Passing through a constructor
    */
    public ReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Using getter/setter methods
In this approach, you would pass the reference of your object through explict public setXX() methods. This approach is more flexible because you can update the reference object as and when you want to (think polymorphism ). As an example:

public class ActualClass{
    private ReferenceClass refClassObject;

    public ActualClass(){
    }

    public void setReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public ReferenceClass getReferenceClass(){
        return refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Using a combination of constructors and getters/setters
For added flexibility, you might want to initialize your Actual class object with a reference. However if you would also want to keep the option of changing the reference at object at a later stage, go for a combination of both #1 & #2 that I specified above.

public class ActualClass{
    private ReferenceClass refClassObject;

    public ActualClass(){
    }

    public ActualClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public void setReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public ReferenceClass getReferenceClass(){
        return refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Which one should you choose? Well it would depend on your implementation and requirement.

This answer is exactlty same as Doug Ramsey this link I tried to explain with the same logic.

  public class A {
        public void m1() {
            System.out.println("inside m1 method");
            ABC ab = new ABC(); // 2 object is made and reference is given to ab 
            XYZ xyz = new XYZ();  3 object is made and reference is given to xyz 
            xyz.send(ab); // 4 THIS IS WHAT YOUR QUESTION MEANS
        }
    }


    class XYZ {
       ABC ab;
       //...
       void send(ABC ab) { // 5
         this.ab = ab;
         System.out.println("inside send");
         callme(); 
       }
       //...
       void callme() { // 6
           System.out.println("A is : "+ab.a);
           System.out.println("b is : "+ab.b);
          // do something with instance variable ab
       } 
    }

public class ABC {
    int a = 10;
    static int b= 20;
    public static void main(String[] args) // called first
    {
        A a = new A();
        a.m1();
    }
}

You have two ways to pass object parameter to one class to another.

  • Passing parameter to a method

     public void passMethod(ABC ab) { } 
  • Passing parameter to a constructor

     public class XYZ { public XYZ(ABC ab) { } } 

I know this question is old, but If I'm correct you want to transfer an Object into another class to be used.

In order to do that you need a few things

Class XYZ has to have a constructor to take in the parameter "Object" it would something like

class XYZ{
    private Object ab
    public XYZ(Object ab){
        this.ab = ab;//This is the constructor called when you create an XYZ object, and want to use the Object ab in XYZ
    }
package demo;

class ABC{
     void disp(xyz arg1){
        System.out.println("runing disp method in pratics");
        System.out.println("x value:"+arg1.x);
        arg1.test();
     }

}

class xyz{
     int x = 67;
     void test(){
        System.out.println("runing test method in pratics");
     }
}

class pratics {

    public static void main(String[] args) {
        ABC ab=new ABC();
        ab.disp(new xyz());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM