• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot make a static reference to the non-static field

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there a way to refer to a non-static field of an outer class from a static nested class?

Please see my code below:



This code throws an error at testLabel.setValue(value) as I am trying to make a static reference to a non-static field. But, I need the value to be non-static and yet reference it in the static nested class's method. How do I do it?

You may notice how I instantiate TestComponent.java here: http://top.cs.vt.edu/~vsony7/patches/gfc.patch

The idea is to create two labels dynamically with two different values "Label 1" and "Label 2" and append them to two different Components i.e. vlayout1 and vlayout2. But, when I run this code, a label gets attached to each of the layouts but the value of both the labels is "Label 2". You can test this at:

The problem is that the two windows from testlabel.zul created by two calls to IncludeBuilder share the static class TestLabel. After the super.doAfterCompoe() the value of test label is set to "Label 2" in both the calls.

I am using Zk framework and ZK does not have an enclosing instance so the inner nested class TestLabel must be static.

Thanks,
Sony

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A non-static field only exists in relation to an instance of the class. So you have to access it relative to an instance of the class. If you're in a non-static inner class, you're already in such an instance, which is why you can access it OK. But in a static inner class that's not the case.

So, which TestComponent object do you want to use the value of?
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Matthew Brown: Please see my edited question. Although, I am using ZK Ajax Framework, I think the issue is still with my java logic and not really ZK specific. I hope my code makes sense to non zk users.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sony vijay wrote:Although, I am using ZK Ajax Framework, I think the issue is still with my java logic and not really ZK specific...


Well, I gave you an answer in your other thread on this subject, but you haven't yet acknowledged it. Perhaps it wasn't what you wanted to hear.

Winston
 
Marshal
Posts: 79471
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sony vijay wrote:@Matthew Brown: Please see my edited question. Although, I am using ZK Ajax Framework, I think the issue is still with my java logic and not really ZK specific. I hope my code makes sense to non zk users.

Don’t edit questions after getting an answer.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with Campbell, don't edit questions. post a reply instead. in an attempt to answer your question, why is the inner class static anyway?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid I don't know anything about that framework, but it doesn't change what I said - a non-static member belongs to a specific instance. If you continue with this approach (and I don't know what the alternatives are) you need to find a way to tell the TestLabel which TestComponent it's using.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic