Reputation: 14179
I have a class named StatisticFrame
. This class creates an object named stats
inside a method. How can I pass this object created from StatisticFrame
class to another class?
Upvotes: 1
Views: 83081
Reputation: 2886
You can pass the created object as argument to another class constructor or it's method.
class Apples{
public static void main(String[] args){
ApplesTestDrive obj = new ApplesTestDrive();
ApplesSampleTestDrive objOne = new ApplesSampleTestDrive();
// Pass created object obj as argument to ApplesSampleTestDrive method.
objOne.paint(obj);
}
}
class ApplesTestDrive{
public String bucket;
public ApplesTestDrive(){
bucket = "blue";
}
}
class ApplesSampleTestDrive{
public void paint(ApplesTestDrive obj){
System.out.println("Paint apple one: " + obj.bucket);
}
}
Upvotes: 2
Reputation: 45
One way :-
Suppose you have one class Xyz The object that you want make the getter method for it and in another class Abc make the object of Xyz and with that object call the getter method and store the return of it in your current class
2nd way :-
use inheritance make the class as super
class Abc extends Xyz By this u will directly gets access to all the objects, methods present in super class
Hope that this make sense to you
Upvotes: 0
Reputation: 1105
Keep a variable that will point to the receiver object (say of type TargetFrame class) in your StatisticFrame class and then invoke the method (say SetStatsObject(object obj)) defined in the receiver object to receive the object.
your StaticFrame class will look like this
class StatisticFrame {
private TargetFrame targetObject = null;
public StatisticFrame (TargetFrame obj) {
this.targetObject = obj;
}
public void Send (Object stats) {
object stats = GetStatsObject(); // this will create and returns stats object
targetObject.SetStatsObject(stats);
}
//...
}
and your TargetFrame (the receiving object's class) should look like this
class TargetFrame {
public void SetStatsObject(Object stats) {
// Do what ever you want with stats
}
// .....rest of the methods follows
}
Upvotes: 4
Reputation: 16
You can create an object for the other class that you want to pass the object with the constructor that accepts your newly created object. Another way you can use some callback functions using interface
Upvotes: 0
Reputation: 7899
There are number of ways you can achieve this . either make the return type of method as stats
and then in other class you can make object of StatisticFrame
and call that method.
StatisticFrame sf=new StatisticFrame();
Stats st=sf.method();
Other way if you don't want to make return type as Stats
then Make global private
variable of type Stats
and assign this in your method, and then one public
getter
method will return this object to other classes.
Upvotes: 1
Reputation: 124215
One of ways is that you can create method in your "another class" that will accept as argument object you want to pass, and you call that method from your base class
class Sender{
public void createAndSend(Reciever reciver){
String s="some data from Producer";
reciver.recieve(s);
}
}
class Reciever{
public void recieve(String data){
System.out.println("I recieved "+data);
}
}
//lets test it
class TestX{
public static void main(String[] args) {
Sender s=new Sender();
Reciever r=new Reciever();
s.createAndSend(r);
}
}
output: I recieved some data from Producer
Upvotes: 1
Reputation: 9914
Another class
needs a method as an argument
that expects your stats
object and call that method from your class and pass your stats object
private void yourmethod(){
Stats stas =
AnotherClass ac = new AnotherClass();
ac.thatMethod(stats);
}
class AnotherClass {
public void thatMethod(Stats stats){
}
}
Upvotes: 1