how would i go about doing this (java)

Started by
7 comments, last by Whackjack 19 years, 6 months ago
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"); } }
http://stowelly.co.uk/
Advertisement
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 hereClassB 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}// constructorpublic 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.
the rug - funpowered.com
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
http://stowelly.co.uk/
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.
Lucas Henekswww.ionforge.com
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.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
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.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
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)
the rug - funpowered.com
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.
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.

This topic is closed to new replies.

Advertisement