• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

unfamilier java syntax

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code doesn't compile. But with some changes it could be a anonymous class with an initializer block.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
Oracle PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic