The results are not guaranteed to be absolutely the same, although in general they will be. Specifically, you can write a methodA
and methodB
implementation that would yield different results when run in Example1
and Example2
, even if the class names of the main program were made the same before execution.
One way to accomplish this would be to generate the stack trace and then introspect on the line number for the execution of methodB
, which is different in Example1
and Example2
.
For example, the below methodB will result in different output when run in Example1
and Example2
.
public static void methodB()
{
int count = 0;
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : elements)
{
count += element.getLineNumber();
}
System.out.println(count);
}
However, in general the programs will yield the same results since this type of logic based on stack traces or other such aspects is unusual.