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.
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:
// Assuming the ZonedDateTime objects, e.g. dateTimeA, are within ClassA's "methodA()" method...
ClassA objectA = new ClassA();
System.out.println(objectA.methodA().dateTimeA);
When this inevitably errored, I figured I would need the method to return 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.
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.
EDIT(S):
Here is more of my code to make the problem more reproducible:
WatchFaceEDIT (i.e. A):
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 "1":
ZonedDateTime dateTimeHere = ZonedDateTime.now();
hour = dateTimeHere.getHour();
minute = dateTimeHere.getMinute();
amPM = dateTimeHere.getHour();
break;
case "2":
ZonedDateTime dateTimeThere = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
hour = dateTimeThere.getHour();
minute = dateTimeThere.getMinute();
amPM = dateTimeThere.getHour();
break;
case "3":
hour = -1;
minute = -1;
break;
}
// The rest of the code in WatchFaceEDIT does some things with these hour and minute variables.
}
}
WatchEDIT (i.e. B):
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("hh:mm a 'on' EEEE, MMMM dd, yyyy");
String dateTimeDisplay = watchFaceObject.watchFaceMethod(userInput).dateTimeHere.format(dateTimeFormat);
// There is more to the code than this, but the problem seems to be here.
}
}
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.