Results 1 to 3 of 3
  1. #1
    olbion is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default [SOLVED] instantiating a class from other classes of different types...

    Hi,

    I have created a class which, when something occurs, should call a method in the class that instantiated it. My solution for doing this so far was to send the instantiator object to the instantiated class constructor, like this:

    Java Code:
    class Test {
    	public Test()
    	{
    		new UsefulClass(this);
    	}
    	
    	public void takeAction()
    	{
    		//do something
    	}
    }
     
    class UsefulClass
    {
    	private Test test;
    	
    	public UsefulClass(Test test)
    	{
    		this.test = test;
    	}
    	
    	public void somethingHappened()
    	{
    		test.takeAction();
    	}
    }
    My problem is that I now also want to instantiate UsefulClass from another class, which is not of the same type. If I simply create an object from the other class in the same way, that is
    Java Code:
    new UsefulClass(this)
    , then java will complain that the type does not match.

    With my limited knowledge of java, I see two solutions, none of which I much like:

    1. To create loops in the instantiating classes which check when a value has changed in the instantiated class, and thus take action.
    2. Create separate constructors for UsefulClass, for each class that is to instantiate it.

    But perhaps there is some much more elegant way to solve this?

    Thank you very much!

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Yes you are right, there is a much better solution. And just by realizing this you show you have the "programmer mind" :). The solution will use interfaces as callbacks.
    Java Code:
    public interface CallBack
    {
           public void action();
    }
    
    class UsefulClass
    {
        private CallBack cb;
    
        public UsefulClass(CallBack callback)
        {
            this.cb = callback;
        }
    
        public void somethingHappened()
        {
            cb.action();
        }
    }
    
    class Test
    {
        public Test()
        {
            new UsefulClass(new CallBack()
            {
                public void action()
                {
                    Test.this.takeAction();
                }
            });
        }
    
        public void takeAction()
        {
            // do something
        }
    }
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    olbion is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Thank you very much. An interface was indeed what I needed.

Similar Threads

  1. Replies: 7
    Last Post: 07-12-2010, 04:24 PM
  2. How to Merge all classes into One class
    By jazz2k8 in forum New To Java
    Replies: 12
    Last Post: 04-23-2008, 03:40 AM
  3. Replies: 3
    Last Post: 04-08-2008, 07:12 AM
  4. How to make Beans Lazily-instantiating beans
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:41 PM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •