title: accessing fields of a class in Java
body: <p>I am completely new to Java. </p>
  
  <p>I was practicing a code about a person eating some fruit. I have 3 classes</p>
  
  <p><strong>Fruit Class:</strong></p>
  
  <pre><code>public class Fruit {
      String fruitname = "grapes";
  }
  </code></pre>
  
  <p><strong>Person Class:</strong></p>
  
  <pre><code>public class Person {
      void eat(Fruit f) {
          System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname
      }
  }
  </code></pre>
  
  <p><strong>Test Class:</strong></p>
  
  <pre><code>public class TestFruit {
      public static void main(String[] args) {
          Person p = new Person(); // person object
          Fruit f = new Fruit(); // fruit object
          p.eat(f);
      } // eat method of person class
  }
  </code></pre>
  
  <p><strong>output:</strong></p>
  
  <pre><code>person is eating grapes
  </code></pre>
  
  <p>For accessing fields of a class, Object of that class is created. </p>
  
  <p><strong>My question is:</strong></p>
  
  <p>In <code>Person</code> class, how can I access <code>fruitname</code> field of <code>Fruit</code> class (i.e., writing <code>f.fruitname</code>) without instantiating <code>Fruit</code> class in <code>Person</code> class?</p>
  
  <p><code>fruitname</code> is a data member of <code>Fruit</code> class and instance member don't exist until object is created.</p>
  
  <p>I have just started learning Java, and I am stuck here. Please help me to understand.</p>