title: How can I access an object within another object's method?
body: <p>I am a (very) amateur Java programmer and I am having some trouble. Essentially, I have 2 classes: A and B, where B extends A.</p>
  <p>In A, I have a method defined that creates some ZonedDateTime objects. I know how to create an instance of A in B so that I can execute A's method in B, but the problem lies later on when I need direct access to that method's ZonedDateTime objects. My first guess was to access them like this:</p>
  <pre><code>// Assuming the ZonedDateTime objects, e.g. dateTimeA, are within ClassA's &quot;methodA()&quot; method...
  ClassA objectA = new ClassA();
  System.out.println(objectA.methodA().dateTimeA);
  </code></pre>
  <p>When this inevitably errored, I figured I would need the method to <em>return</em> the objects, but from what I can tell, this is not possible; you can return only certain data types, not objects. I should also mention that I need the objects to remain as objects, so converting them to Strings then returning them that way is not going to work for me.</p>
  <p>This issue feels very basic, but even so I couldn't find any answers elsewhere; I'm sorry if this is a duplicate question. Any help would be greatly appreciated.</p>
  <p>EDIT(S):</p>
  <p>Here is more of my code to make the problem more reproducible:</p>
  <p>WatchFaceEDIT (i.e. A):</p>
  <pre><code>import java.time.ZonedDateTime;
  import java.time.ZoneId;
      
  public class WatchFaceEDIT {
      
      // Up here are some variable declarations and things used in the switch statement below.
      
      public void watchFaceMethod(String userInput) { // userInput is a parameter passed up from a subclass of this class.
          
          switch (userInput) {
              case &quot;1&quot;:
              ZonedDateTime dateTimeHere = ZonedDateTime.now();
              hour = dateTimeHere.getHour();
              minute = dateTimeHere.getMinute();
              amPM = dateTimeHere.getHour();
              break;
              
              case &quot;2&quot;:
              ZonedDateTime dateTimeThere = ZonedDateTime.now(ZoneId.of(&quot;Europe/Paris&quot;));
              hour = dateTimeThere.getHour();
              minute = dateTimeThere.getMinute();
              amPM = dateTimeThere.getHour();
              break;
              
              case &quot;3&quot;:
              hour = -1;
              minute = -1;
              break;
          }
          // The rest of the code in WatchFaceEDIT does some things with these hour and minute variables.
      }
  }
  </code></pre>
  <p>WatchEDIT (i.e. B):</p>
  <pre><code>import java.time.format.DateTimeFormatter;
  
  public class WatchEDIT extends WatchFaceEDIT {
      
      static void watchMethod(String userInput) {
  
          WatchFaceEDIT watchFaceObject = new WatchFaceEDIT();
          watchFaceObject.watchFaceMethod(userInput);
  
          DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(&quot;hh:mm a 'on' EEEE, MMMM dd, yyyy&quot;);
          String dateTimeDisplay = watchFaceObject.watchFaceMethod(userInput).dateTimeHere.format(dateTimeFormat);
          // There is more to the code than this, but the problem seems to be here.
      }
  }
  </code></pre>
  <p>I will briefly mention here that I thought the problem could have to do with the scope of the switch statement, but in WatchEDIT I was able to run watchFaceMethod without problems, and the method does utilize the created objects to display things.</p>