• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enthuware question doubt and smth else

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a question in the Enthuware's mock:
Annonymous inner classes cannot be static.
And it's true regarding to enthuware answer.
So what about it:

OR


And my second doubt(that smth else ):


BUT:

IS OK

So there can be no blink static finals unless there is static initialization block. Blink static final can be initialized only at the time of referencing or inside the static initialization block. So static variables do not get their default values?

Correct me if I am wrong.

Whoa, quite a long post.
Thanks for replying.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lukas Smith wrote:So static variables do not get their default values?


Class fields whether static or non-static, don't get a default value if they are final.

Also the example that you gave of static anonymous class is wrong, first of all the code won't compile as you closed Test class after m method so the declaration of T is invalid. Also that anonymous inner class is not static. You can try this to confirm

 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't follow this - I have no problems compiling and running with a static anonymous inner class. Obviously though, I cannot invoke any new methods or members defined within that anonymous declaration; just what appears on the reference type.So why do Enthuware say an anonymous inner class cannot be static? I am worried I have a serious hole in my understanding.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason in your code, the anonymous inner class is not static. Only the reference variable anonJunk is static. Static inner classes can declare static fields in them. But if you try to add static fields to your anonymous inner class, then it will show an error, that proves that your anonymous inner class is not static...
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, right. I get what is being meant now.

The anonymous inner declaration that cannot be static, but the reference can be. After all, unless you have an instance of that anonymous inner class, you have no way to reference it and thus no way to access any static members that you might try to put in there.

Makes sense.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, it is clear. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic