1

Currently working on trying to have two objects interact with one another; such as having "people" (the objects) talk to each other. My question is, if I have generated two random objects, or "people" containing different attributes, how can I have them interact with each other in my class if they are both static? I am just not sure how to pass them to one another if I have to generate them in a static way.

To keep things simple, let's say I just want to have them introduce themselves. That is have person A say hello to person B, and have person B reply with "Hi Person A, my name is B".

Here is my generateAgent() method:

public class Agent {

  public static Object generateAgent() {
    //Create static object randomAgent
    Agent randomAgent = new Agent();

    //Create this agent's properties:
    //1. Get a Gender/Name
    randomAgent.getName();

    //More Attributes below....
    return randomAgent;
  }
}

Here is my sayHelloTo() method:

public void sayHelloTo(String name) {
  //More Code here
  return;
}

Then in my other java file how do I pass Object A / B (representing person A / B) to my Agent class if they are both static? I'm not sure what I am missing exactly.

public class DiscussionDirector {
  public void discuss() {

    //Create Two People:
    Agent a = new Agent();
    a.generateAgent();

    Agent b = new Agent();
    b.generateAgent();

    //Have them introduce themselves:
    a.sayHelloTo(); // ----> Not sure what parameters and such go here to pass successfully

    return;
  }
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102

4 Answers4

1

You have some fundamental misconceptions about what is going on here (note comments):

public class Agent {
  public static Object generateAgent() {
    Agent randomAgent = new Agent();//This creates an object on the HEAP
    //eliding everything else
    return randomAgent;//This returns the newly created object
  }
}

Now lets look at the invocation of generateAgent():

public class DiscussionDirector {
  public void discuss() {
    Agent a = new Agent();//Here you create a new agent on the HEAP
    a.generateAgent();//And here you call into the Agent class to do the same thing
      //note that generateAgent returns an Object that you're 'forgetting' about.
    return;
  }
}

So lets back up a bit. The static function generateAgent is what is called a 'Factory Method', that ensures an object is created 'properly' (however the programmer defines that) and gives an instance of that object back. Your signature for this is therefore wrong, because it says you're giving back an Object and not an Agent. It should be:

public static Agent generateAgent() {//Declares the type of object being given back
  Agent randomAgent = new Agent();
  //eliding everything else
  return randomAgent;
}

Note that this function can go in any class that knows about the Agent class, because it's static; it does not require an instance of Agent in order to be run. Static methods are also generally invoked on the Class, not an object instance:

Agent a1 = Agent.generateAgent();//sets a1 to the returned result of generateAgent()
  //this is an Agent object. 
Agent a2 = a1.generateAgent();//sets a2 to a DIFFERENT, new agent object
  //At this point there are two instances of Agent on the heap
  //This form of invocation is generally reserved for non-static methods
    //Usually a warning will be generated, too, by your IDE.

Remember to think carefully about what a function consumes (the parameters) and what it returns. If it returns void, then the point of the function is whatever it is doing internally, but if it is something else you can consider the function to convert the inputs to the output. In this case you're converting nothing to a new Agent.

To properly understand this, think of Class definitions (public class Agent...) as blueprints of objects. Any static method in these classes can be executed without any instance of the method existing. Any time you write new Agent (or new anything) you are creating a new instance object on the Heap (the part of memory that is not the stack). Non-static methods can only be invoked on instances of objects. See this Answer for more.

Community
  • 1
  • 1
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • OK wow thank you! That was very helpful and insightful. I've got everything working for the moment, hopefully I can keep going and get the rest of my dialogue running now. – user2925439 Nov 20 '13 at 00:57
1

You likely don't mean to use a static generateAgent() method but a constructor instead. See the following:

public Agent()
{
    this.setName("new Name");
    //more attributes
}

Notice that in a constructor, you do not use a return type such as void or Object and it must match the name of the class. The constructor will be called automatically when you do Agent a = new Agent();

This way, you don't need the unnecessary a.generateAgent();

I'm sure you notice I use a setName() function to apply the name. This is standard Java naming convention to make sure other Objects are setting and getting the attributes correctly. You would likely have a String variable called name and a "getter" and "setter" under Agent like so:

public String getName()
{
    return this.name;
}

public void setName(String newName)
{
    this.name = newName;
}

Now you could keep your sayHelloTo code the same, but under the DiscussionDirector you would want to call the function like this: a.sayHelloTo(b.getName());

There's a lot of ways to expand on anything I said, so if you have any further questions just let me know.

DoubleDouble
  • 1,493
  • 10
  • 25
-1

Your lines

Agent a = new Agent();   //creates an instance of Agent in variable a
a.generateAgent();       //creates another instance which is discarded
                         //this static method unnecessarily called on an instance
                         //the method is static, but there is no such thing as a 
                         //static object, only object instances.

Agent b = new Agent();   //creates a third instance
b.generateAgent();       //creates a fourth instance, this one thown away

a.sayHelloTo();     //method called on the first instance you created

You might want a method with a parameter, and call that on "a" and pass "b". The object can keep a reference to the other object, and they can call methods on the other.

Maybe something like this:

public class Agent {
    Agent neighbor;
    String name;

    public Agent(String myName) {
        //initialization code
        name = myName;
        neighbor = null;    //not strictly necessary, but included for clarity
    }

    public void introduce(Agent other) {
         neighbor = other;
    }

    public String getName() {
        return name;
    }

    public String getNameAndNeighbor() {
        if (neighbor==null) {
            return "my name is "+name+" and I have no neighbor";
        }
        else {
            return "my name is "+name+" and my neighbor's name is "+neighbor.getName();
        }
    }
}

And the following main code:

Agent a = new Agent("Harry");
Agent b = new Agent("Joe");
a.introduce(b);
b.introduce(a);
System.out.println( a.getNameAndNeighbor() );

//produces "my name is Harry and my neighbor's name is Joe"

Each of these objects has a member 'neighbor' which references the other object, and allows them to call methods on the other.

AgilePro
  • 5,588
  • 4
  • 33
  • 56
-3

http://www.seas.gwu.edu/~drum/java/lectures/module4/module4.html

That website has a good example. But basically you need to create an instance object for the people to communicate. The problem here is that public void discuss() isn't static. If it is static, then it can inherit from your other class Agent.

jSeesFor3ver
  • 83
  • 4
  • 16
  • You'll need to explain further, particularly about whatever it is you're trying to say about the inheritance of static methods?! – Dave Newton Nov 20 '13 at 00:06