That tutorial is horribly out of date if it recommends using java.util.Date. A dreadful class, which was largely superseded by Calendar nearly twenty years ago, and should have been completely superseded by newer classes about five years ago. It also doesn't tell you that you oughtn't to use newInstance() any more.
Anyway, Date is a public class in its package and it has a public constructor without any arguments, so it is accessible to newInstance(). So far, so good. So tried your code and didn't get any exceptions.
I'm guessing that direct access to private members of nested/enclosing classes was implemented through bridge-methods before Java 11. Bridge methods are added by the compiler, so they don't account for reflective access.
Java 11 added special access scopes that allow access to these private members without using bridge methods, which would also work when using reflection.
From the JEP I linked to above:
JEP 181 wrote:
A further consequence of the lack of JVM support for private access within a nest, is that core reflection also denies access.
A reflective method invocation (using java.lang.reflect.Method.invoke) from one nestmate to another throws IllegalAccessError (unless access control has been disabled).
This is surprising given that reflective invocations should behave the same as source level invocations.
Stephan van Hulst wrote:The error message tells you exactly why it can't create an instance of NewInstanceTest1$Dog: It has the private access modifier.
I didn't understand Stephan's response here because I thought that as far as inner classes are concerned, the outer class can see members of an inner class, even if they are declared private. For example:
the dog barks. Therefore, from my initial post
I was thinking that because Dog is an inner class, the outer class should be able access all it methods. Is the problem that it's Dog that's the inner class, not dogClass?
Reflection in general is more limited than compiled code. While you can easily call a private method directly, if you retrieve that method through reflection you must first make it accessible (setAccessible(true)).