title: JAVA How to use aspects of newly returned object in a non-static method of same class
body: <p>Im working on an assignment where you create an object " agent" , with about half a dozen attributes that are randomly assigned through random generators in set and get methods. </p>
  
  <p>the big problem I am having as that I keep getting an "Agent1 cannot be resolved" error any time I try to construct a method involving Agent1</p>
  
  <pre><code>   public class Agent 
  {
  // properties of Agents-to-be, set to private to keep things safe
     private String Gender;
     private String Birthday;
     private String Name;
     private String CityBorn;
     private String CityNow;
     private String Major;
     private String filename;
     private String Age;
  // sets 3 variables that will be used throughout the program   
     final String M = "Male" ; 
     final String F = "Female" ; 
     final Random Schrodinger= new Random();  
  
  
  
  
  public static void main(String[] args) 
     {    
      generateAgent();
  
     Agent1.WhoAreYou();
     }   
  
  // this Method invokes all the set methods(not shown) that set the value of the above 
  // private strings. 
     public static Agent generateAgent() {
         Agent Agent1 = new Agent();
         Agent1.setGender(); 
         Agent1.setBirthday();
         Agent1.setName();
         Agent1.setCityBorn();
         Agent1.setCityNow();
         Agent1.setMajor();
  
  
         return Agent1;
     }
  </code></pre>
  
  <p>This next method is the one I am having trouble with. No Matter how I change it, I always get the error message of "Agent1 Cannot be resolved" , which I think just means that the object Agent 1 stops existing outside of my static method. This method is only used for this part of the assignment, in a later part I will have to call on the generateAgent method to create two instances of agents to talk to each other. Otherwise, I've tried combining both of the methods and it seems to work that way, so I just need a way to conserve me object through them. </p>
  
  <pre><code>public void WhoAreYou()
     {
  
         Agent1.getGender();
         Agent1.getBirthday();
         Agent1.getName();
         Agent1.getCityBorn();
         Agent1.getCityNow();
         Agent1.getMajor();
         Agent1.getMajor();
  
         Agent1.sayHello();
         Agent1.sayCityBorn();
         Agent1.sayGender();
         Agent1.sayCityNow();
         Agent1.sayMajor();
  } 
  </code></pre>
  
  <p>Here is an example of one of each of the set and get methods, respecively, in case you think that they are the reason for my problem. they are all very similar </p>
  
  <pre><code>// The set Methods. These methods either use a random number generator to create random values for our
  // agents properties, or use the WordList class to do so.  
  
    public void setGender() 
    {    
        // next.int(n) gives a random number between 0 and n-1, so nextInt(2)'s range is 0-1
        int i = Schrodinger.nextInt(2);
        // "if/else" statement, shortened for readability  
        this.Gender = ((i==1)?M:F); 
    }   
  /** the "get" methods.These are all one line long and are just used to supply inputs for later code 
   *  All of them return "this.('methodname - get')." The this. prefix serves to insure each method is
   *  only returning a characteristic of one particular object
   **/
  
    public String getGender()
    {
       return this.Gender;
    }
  </code></pre>
  
  <p>The "sayX" all rely on the outputs of the previous two methods, and work when i have them in the same method as both set and get </p>
  
  <pre><code> public void howOldAreYou()
    {
        System.out.println( this.Name + " says: I am " + this.Age + " Years old ") ;
    }   
  </code></pre>