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: Objects sharing data

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Objects sharing data

    I am working on an assignment where I write one class that extends a pre-made class. I only have control of the one class file I create and I was wondering if there is any way to share data between all the different objects created from my class? Can I create a file have them all read and edit it? Or possibly have some kind of global array?

    Thanks!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Objects sharing data

    You can pass these instances of other classes a reference to your data. You do this in their constructor or methods. Once they have this reference they haz your data.

    An alternative to consider: this class of yours could have methods that the instances of these other classes can call to have things done with the data without actually getting their hands on the data itself. Why? Because when lots of instances have access to the data it can become very hard to control and have confidence in the integrity of the data. Your bank will likely give you an electronic way of making deposits and withdrawls (methods) but they don't give you access to the "balance" data. And this is true even when you are the author of these other classes. Give other classes the abilty to tell you to do something with the data, don't let them get their careless or anarchic mitts on the data itself: there's more control that way.

    ----

    All that is as relevant (or not) whether or not your class subclasses somethng else. So perhaps I'm not getting something about the actual situation you face and what your're trying to do.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Objects sharing data

    If you are interested, this is the assignment:

    CSE142 handout #18

    It's a game where I am competing with other extensions of the critter class with various attributes.

    I am writing one class of my own design that is an extension of the critter class. I can control direction, interactions and look at the spaces around each object. Ideally I want all my objects to gather as much data about their surroundings as possible and be able to share and see the data from all my other objects. So as you see all the code I have access to is my Husky.class file when the graders run it.

Similar Threads

  1. BoxLayout: Sharing ISN'T Caring... :(
    By snowguy13 in forum AWT / Java Swing
    Replies: 5
    Last Post: December 24th, 2011, 10:21 AM
  2. Extending a Servlet and sharing HttpServletRequest
    By mastervh in forum Java Servlet
    Replies: 6
    Last Post: October 12th, 2011, 03:51 AM
  3. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  4. 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
  5. Sharing my ByteStream, feel free to improve!
    By Verficon in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2011, 09:44 AM