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.

Results 1 to 3 of 3

Thread: Sharing the particular object

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Sharing the particular object

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sharing the particular object

    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:
    SharedObject sharedObject = new SharedObject();
    NewObject newObject = NewObject( sharedObject );
    Alternatively, you can create a setter method in each of the classes that will share the object that sets the shared object:
    public void setSharedObject( SharedObject sharedObject )
    {
        this.sharedObject = sharedObject;
    }
    And there are probably other ways, but these are the most common.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    mohamed_mashood (June 27th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sharing the particular object

    Thank you your post is very useful to me

Similar Threads

  1. Problem sharing objects between threads
    By pepzi999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 18th, 2012, 04:02 PM
  2. Objects sharing data
    By masterkale in forum Java Theory & Questions
    Replies: 2
    Last Post: March 5th, 2012, 12:00 AM
  3. BoxLayout: Sharing ISN'T Caring... :(
    By snowguy13 in forum AWT / Java Swing
    Replies: 5
    Last Post: December 24th, 2011, 11:21 AM
  4. Extending a Servlet and sharing HttpServletRequest
    By mastervh in forum Java Servlet
    Replies: 6
    Last Post: October 12th, 2011, 03:51 AM
  5. By using java how to listout sharing files in LAN...?
    By ram07 in forum Java Networking
    Replies: 4
    Last Post: March 26th, 2011, 10:18 PM