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 9 of 9

Thread: Please help, newb with a simple question...

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help, newb with a simple question...

    Hi, I am new to programming, and trying to learn by making a few simple programs.

    My problem involves passing a reference between classes.

    I learned how to pass references via constructors, but now I have run into a dilemma. I want to pass a reference of two objects to each other, but have run into the problem that one must be created before the other in order to reference it. it's kind of a chicken and egg situation.

     
    	  TimerPanel timer = new TimerPanel(input);
    	  InputPanel input = new InputPanel(control);
     
    	  panel.add (timer);
    	  panel.add (input);

    The constructors of each class will accept a reference of the other, but obviously, with this code, when I try to pass the variable input to the new TimerPanel, it cannot find input because it hasn't been created yet, and tells me the symbol cannot be found.

    Does anyone know how to fix this? Any help would be greatly appreciated. Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help, newb with a simple question...

    Can't you just use a setter method in one of them?

    But many times, this is a sign of a bad design. Are you sure you're instantiating things where they really belong?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, newb with a simple question...

    You're probably right, it's probably a bad design. Like I said, I'm very new, so I'm kind of trying to play by ear... I guess I'll reevaluate my design. But I don't even know what a setter method is, thank you for your suggestion, I'll look it up and find out. if you have the time, it would be helpful if you could give me a quick example of what something like that would look like to get me started though.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help, newb with a simple question...

    A setter is just a method that takes a parameter, then sets something in the class based on that parameter. It would probably work exactly like your constructor.

    Recommended reading: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Or you might want to instantiate one of the classes inside the other, then call a getter from the third class when appropriate. It really depends.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, newb with a simple question...

    Why not just declare and define input before timer. Then you wouldn't have the issue.

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, newb with a simple question...

    Quote Originally Posted by DougFane View Post
    Why not just declare and define input before timer. Then you wouldn't have the issue.
    Ah, sorry, I had a typo in my code...

    it's supposed to be:

     
    	  TimerPanel timer = new TimerPanel(input);
    	  InputPanel input = new InputPanel(timer);
     
    	  panel.add (timer);
    	  panel.add (input);

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, newb with a simple question...

    I guess it depends, if you actually need information from one in order to create the other, then you need to rethink your whole design. However, if you simply need to pass one object to the other's constructor (say you need a TimerPanel to make an InputPanel, but not vice versa) you could write the code like this:

     
    TimerPanel timer;
    InputPanel input;
     
    timer = new TimerPanel(input);
    input = new InputPanel(input);

    hope this helps

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help, newb with a simple question...

    Quote Originally Posted by DougFane View Post
    I guess it depends, if you actually need information from one in order to create the other, then you need to rethink your whole design. However, if you simply need to pass one object to the other's constructor (say you need a TimerPanel to make an InputPanel, but not vice versa) you could write the code like this:

     
    TimerPanel timer;
    InputPanel input;
     
    timer = new TimerPanel(input);
    input = new InputPanel(input);

    hope this helps
    What would passing input into its own constructor do? What would passing it into TimerPanel's constructor before it's initialized do? Depending on how things are defined, that might even result in a compiler error.

    I appreciate that you're trying to help, but providing incorrect information is not helpful.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help, newb with a simple question...

    I had a typo as well. It was meant to be InputPanel = new InputPanel(timer);

    I wasn't really thinking about the fact that the timer object hadn't really been created yet. My bad.

Similar Threads

  1. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  2. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 08:17 PM
  3. Help with a simple question
    By allea in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 28th, 2011, 07:46 AM
  4. Just a simple question
    By newbie in forum Java Theory & Questions
    Replies: 10
    Last Post: December 6th, 2010, 08:19 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM