Richard Hayward

Ranch Hand
+ Follow
since Feb 15, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Richard Hayward

Thank you for the explanation Mike, I think I understand the situation now.

Mike Simmons wrote:So here, you're trying to call getClass() on a reference of type Inner.  Which is a private class.  The compiler won't let you call any methods on a private class, from outside the top-level class containing the private declaration.  



Evidently even those methods which are inherited unchanged from a non-private ancestor.

In this case, Inner descends directly from non-private Object, which is where getClass() is defined. Inner doesn't override getClass() and yet because Inner is private getClass() can't be called in this example.
7 months ago
Hi Campbell,

Campbell Ritchie wrote:That is equivalent to calling new outer.Inner().getClass().... where the Inner class is not a true inner class because it is static. You can't access members of such a private nested class from outwith its enclosing class.


Could you expand on that a bit?

Referring to lines 13-15 of my initial post, I still don't see why these two lines compile

But written as a single line, javac gives an error.

I don't see the connection you mention with static classes. The inner class in my original example isn't static.
7 months ago

Campbell Ritchie wrote:That is equivalent to calling new outer.Inner().getClass().... where the Inner class is not a true inner class because it is static. You can't access members of such a private nested class from outwith its enclosing class.


Hi Campbell. Apologies, but are you referring to the code I posted, or that of Piet?
7 months ago
This code doesn't compile:

GetInner.java


Without line 15, it compiles & runs


But with line 15, which to me just combines the two previous legal lines into one, compilation fails.



Could anyone tell me why?
7 months ago

Stephan van Hulst wrote:
Polymorphism allows you to swap the actual type of an object that a variable refers to without changing the validity of the program.


So maybe I have a mistaken idea of what polymorphism means. Here, I 'swap the actual type of an object that a variable refers to'.


At line 18, instead of using a new Animal(), I use a new Dog() and the code still works.

It still seems weird though. By referencing the Dog via an Animal variable, I can invoke the Dog's makeNoise method with arguments that it shouldn't accept.
The compiler wouldn't allow:


I've noticed a similar thing with interfaces. An interface method with an int... parameter can be implemented by a method with an int[] parameter.

For example


7 months ago

Carey Brown wrote:No, that is not an override


ok, I thought @Override at least gave a compile time message if the annotated method wasn't overriding something.

For example


Line 12 is ok because Dog.eat() does indeed override Animal.eat()

but javac gives an error at line 16 because Dog.bark() isn't overriding anything.

7 months ago
A Dog is an Animal, so I'm expecting that anything an Animal can do, a Dog should be able to do.


But here, that seems to not be the case. Although I can call animal.makeNoise("C", "D"), that same method call on dog won't compile.

Does that not break polymorphism?
7 months ago

Campbell Ritchie wrote:I suggest you add a retention to your annotation and see what happens.



Changing the annotation so as to include that:

MyExercise.java


PrintMyAnnotations.java


It works!



Seems that without RetentionPolicy.RUNTIME, the annotation doesn't exist at runtime.

Thanks Campbell
10 months ago
I'm reading Jeanne & Scott's complete OCP 11 study guide, chapter 13 about Annotations.

Following the syntax they give, I define an Annotation:

Exercise.java


I then create a class, with that Exercise annotation applied, than can print out it's own annotations:

PrintMyAnnotations.java


but nothing gets printed


If, instead of @Exercise, I use a @Deprecated annotation


I get, as expected, an annotation reported:


Could anyone tell me why, in the case of @Exercise, I'm not getting any annotation reported?

10 months ago
Thanks to all for your help on this thread.

Seems I should spend a bit of time exploring what levels of access are given to objects when reflection is used, as opposed to when it's not.
5 years ago
So could I clarify this?

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?


5 years ago
Thanks for the tips about Date. It's not primarily Date I was interested in, any class would do. I just came across that code snippet when googling for an example of the use of newInstance().

Campbell Ritchie wrote: It also doesn't tell you that you oughtn't to use newInstance() any more.


ok. That link for the java 12 api shows newInstance as deprecated since java 9. I'm using a java 8 compiler.

Campbell Ritchie wrote:
So  tried your code and didn't get any exceptions.


Should still work though shouldn't it?

5 years ago
Could anyone tell me why one of these examples works and the other doesn't?

My first example compiles but throws an exception.

According to the api for Class
'IllegalAccessException is thrown  if the class or its nullary constructor is not accessible.'

I'm thinking that the fact that the dog barks at line 7 demonstrates that neither of those things is true.

My second example, (simplified from the example at TutorialsPoint ) compiles and runs ok.


Seems to me however that the second example is doing the same thing.
5 years ago
I'm thinking of doing the upgrade exam from java 6 to 8.

I liked the Sierra/Bates book when I did the java 6 exam, so I thought I'd stick with those authors.

Could anyone with experience of that book comment on a couple of questions?

Assuming I have a good grasp of the material required for the java 6 exam, is it reasonable to start with volume II? Or, would I find that volume II assumed knowledge of some post java 6 feature that had already been explained in the first volume?

Secondly, does the book contain a checklist of what pages/chapters need to be mastered for the upgrade exam? The preview at Amazon cuts off at the point where my guess is that any such table would be.

Liutauras Vilda wrote:
But that is really what would happen. s[0] trying to access very first element within an array, while s isn't referring to an array of Strings, but rather a singular String.


oops.. apologies!  I should have said that the test had several questions  like this:

Key point being that an isolated bit of code like this won't compile. Put it inside a class and it will compile but throw an exception at runtime.
I wasn't expecting incomplete code samples like this.
7 years ago