title: Java calling object in method outside of main
body: <p>I have a simple problem that I've been stuck on for some time and I can't quite find the answer to.  Basically, I'm creating an object and trying to access the variables without using static variables as I was told that is the wrong way to do it.  Here is some example code of the problem.  I receive an error in the first class that can not be resolved to a variable.  What I would like to be able to do is access <code>t.name</code> in other methods outside of the main, but also in other classes as well.  To get around this previously I would use <code>Test2.name</code> and make the variable static in the <code>Test2</code> class, and correct me if I'm wrong but I believe that's the wrong way to do it.  Any help would be greatly appreciated =)</p>
  
  <pre><code>public class Test {
    public static void main(String[] args) {
      Test2 t = new Test2("Joe");         
    }
  
    public void displayName() {
      System.out.println("Name2: " + t.name);
    }
  }
  
  public class Test2 {
    String name;
  
    public Test2 (String nm) {
      name = nm;
    } 
  }
  </code></pre>