Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-04-2008, 12:45 AM
Member
 
Join Date: May 2008
Posts: 7
olbion is on a distinguished road
[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:

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
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-04-2008, 01:04 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
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.
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 @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 12:55 PM
Member
 
Join Date: May 2008
Posts: 7
olbion is on a distinguished road
Thank you very much. An interface was indeed what I needed.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Merge all classes into One class jazz2k8 New To Java 12 04-23-2008 05:40 AM
[SOLVED] Using classes and overriding one class for another StealthRT New To Java 3 04-08-2008 09:12 AM
javax.comm:Error instantiating class com.sun.comm.Win32Driver bbq Advanced Java 3 02-07-2008 11:41 PM
How to make Beans Lazily-instantiating beans JavaBean Java Tips 0 09-26-2007 10:41 PM
problem with scanner class:incompatible types fred New To Java 1 07-20-2007 09:02 AM


All times are GMT +3. The time now is 10:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org