Hi all,
I am creating stand alone application in java
i want to know how to share a particular object for all classes through out project
Thank you
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi all,
I am creating stand alone application in java
i want to know how to share a particular object for all classes through out project
Thank you
It depends on what the shared object is being used for. The Java tutorials provide a good example of using a class variable as a counter, how many bicycles (or something) are in the inventory, but that's a simple int, I believe.
If you want one of your own class objects usable by other classes in the project, you can pass the object to each class' constructor as an instance of the class is created:
Alternatively, you can create a setter method in each of the classes that will share the object that sets the shared object:SharedObject sharedObject = new SharedObject(); NewObject newObject = NewObject( sharedObject );
And there are probably other ways, but these are the most common.public void setSharedObject( SharedObject sharedObject ) { this.sharedObject = sharedObject; }
mohamed_mashood (June 27th, 2013)
Thank you your post is very useful to me