• Post Reply Bookmark Topic Watch Topic
  • New Topic

unfamilier java syntax  RSS feed

 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I recently came across the below java code for getList(). can anyone help me debugging it. I can't understand the syntax. if anyone have simple example of such syntax, I'd be obliged.


 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code doesn't compile. But with some changes it could be a anonymous class with an initializer block.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please elaborate on what your question is. The method looks fine to me.

What is it that confuses you? I hope we can assume you are familiar with equals signs (=), braces ({}), and some other basics. Are you confused by the angle brackets? the 'static' in the declaration?

Are you sure it is the syntax that bothers you?

It really helps to have an actual question...

rc
 
Manjula Weerasinghe
Greenhorn
Posts: 25
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It (in line 13) is an anonymous inner class with instance init block. But your code does not compile it has other errors.
Since this inner class is declared in a static method, you need to define your "contract" variable as a static variable.

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't need to be static; it can also be a) a final local variable or final parameter in the method, or b) an instance variable of the enclosing class (or the enclosing class' enclosing class if that exists, etc).
 
Manjula Weerasinghe
Greenhorn
Posts: 25
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:It doesn't need to be static; it can also be a) a final local variable or final parameter in the method, or b) an instance variable of the enclosing class (or the enclosing class' enclosing class if that exists, etc).

Hi Rob,

Since "contract" variable is reassigned several times to different objects in this example, you cannot use a final local variable or final method parameter inside the inner class as the "contract" variable. But you may use any other final local variable or a final method parameters inside the inner class as long as you keep the meaning of the "final" modifier (not reassigning it to a different value/reference).

Since this inner class has defined inside a "static" method, you can only access the static variables of the enclosing class (I am not very good at English, but as I know this means outer class (MockClass), correct me if I am wrong) and you cannot access the instance variables of the enclosing class, because there is no reference to "this" (current object) inside a static method.

Thanks & Regards,
Manjula
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're absolutely right. I missed the static part, and overlooked the assignments. So yes, you're right, contract must be a static field of one of the enclosing classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!