Sign in to follow this  
Stowelly

how would i go about doing this (java)

Recommended Posts

Stowelly    156
ok this is my basic program it runs from this main function
Quote:
public static void main(String[] args) { System.out.println("Starting main method in Wk1Ex2..."); ClassA myClassA=new ClassA(); ClassB myClassB=new ClassB(); myClassA.query(); myClassB.query(); myClassB.queryClassB(); myClassA.queryClassB();
and these are the 2 class definitions i am using... i have only shown one of them as ClassB is exactly the same. all i am trying to run the query function on classA from classB and vica versa but i cant see how this is possible.... thanks alot
Quote:
class ClassA { //attributes and methods in here private int noOfQueries=0; private String name="A"; public void query(){ System.out.println("\nQuery result: My name is '"+name+"' Previous queries = "+noOfQueries); noOfQueries++; } public void queryClassB(){ // System.out.println("\nSorry, object A cannot query object B. Why not?"); } // constructor public ClassA(){ System.out.println("\nA new object has been instantiated from ClassA"); } }

Share this post


Link to post
Share on other sites
The Rug    628
Your class does not have an instance of the other class to operate on- you will have to pass it one.


ClassA myClassA;
ClassB myClassB;
myClassA = new ClassA(myClassB);
myClassB = new ClassB(myClassA);

myClassA.query();
myClassB.query();

myClassB.queryClassA();
myClassA.queryClassB();


Then


class ClassA {
//attributes and methods in here
ClassB classBInstance;

private int noOfQueries=0;
private String name="A";
public void query(){
System.out.println("\nQuery result: My name is '"+name+"' Previous queries = "+noOfQueries);
noOfQueries++;
}

public void queryClassB(){
// you can query your instance of class B here
}

// constructor
public ClassA(ClassB classBInstance){
this.classBInstance = classBInstance;
System.out.println("\nA new object has been instantiated from ClassA");
}
}


Class A now has an instance of class B to query.

That should work, but I don't have my compiler installed and my Java is a little rusty :/

Depending on what information you want from the other class, you could consider using static members.

You could also pass an instance of the class you wish the query into the function queryClassB, ie. myClassA.queryClassB(myClassB); and operate on that.

Share this post


Link to post
Share on other sites
Stowelly    156
thanks alot it all works perfectly except this part

ClassA myClassA;
ClassB myClassB;

myClassB=new ClassB(myClassA);
myClassA=new ClassA(myClassB);

if i initialise classB first i cant parse ClassA and vic versa... any idea about this please mate

Share this post


Link to post
Share on other sites
lucinpub    186
That is the sign of a very poor design. If this is your project and not just some exercises from a book or something, you need to rethink the design of your system.

Share this post


Link to post
Share on other sites
When you are passing classes in Java, what you are really passing is the POINTER to the class. So when you initialize A first, with the argument of B, A gets a null pointer in whatever variable should hold the reference to B, which is bad. So instead do something like this.

ClassA myClassA = new ClassA();
ClassB myClassB = new ClassB();

myClassA.setInstance(myClassB);
myClassB.setInstance(myClassA);


With each class having a member variable of the other's type, and the setInstance function setting that variable to the value passed. That should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by lucinpub
That is the sign of a very poor design. If this is your project and not just some exercises from a book or something, you need to rethink the design of your system.


When only considering "good" code verses "bad" code, I agree 100%. That being said I have written a Sokoban Clone (In Java) where virtually every class was connected to it's "owner" (the main class) to retrieve data to perform calculations. It was easier than passing a bunch of variables for every function call. The game worked, and I might still have it.

Share this post


Link to post
Share on other sites
The Rug    628
Cheers vampire, thought I'd forgotten something :rolleyes:

On the subject of bad code, for small personal projects I wouldn't put too much emphasis on good code, I'd rather get the thing done quickly. As long as you dont get sloppy mind.. and dont ever tell your teacher that :o)

lucinpub, in what way is it a "very poor design"? It might not be some uber-classy system, but its simply a "has a" relationship, where classA has a classB and vice versa... it isnt a hack and you arent "breaking" anything... (although I could be wrong, as always)

Share this post


Link to post
Share on other sites
Zahlman    1682
Because that kind of object "marriage" (mutual has-a) is a cyclic dependancy; depending on the context, it may be better to break one link or the other (and pass a 'parent pointer' where needed), or merge the objects.

Share this post


Link to post
Share on other sites
Whackjack    100
Quote:
Original post by Venerable Vampire
When you are passing classes in Java, what you are really passing is the POINTER to the class.


You said the "P" word. Beware the Java gods who will smite thee for blasphemy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this