Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700
Accepted Solution

Cannot use "this" in a static context

Hi,

 

I am using OS5 and Eclipse Galileo.

 

I built a method, which is static, bacause it will be called from another class.

But in the code of the static method I ve got the following lines:

 

manualDate = new DateField("", initial.getTime().getTime(), myDtTm);
manualDate.setChangeListener(this);

 Within the second line I get an error from eclipse:

 

"Cannot use this in a static context"

 

What can I do?

 

Thanks for any help.

 

cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.
Highlighted
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Cannot use "this" in a static context

read "java is not (only) an island".

or "teach yourself java in 21 days".

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700

Re: Cannot use "this" in a static context

Hi simon,

 

nice idea, but I am in a deadlock.

 

It is clear to me, that I cannot use "this" in that way, but I  don't know how to get further.

 

What I am doing is the following: I am invoking calendar, due to the fact I have to wait for onfocusnotify, if my application gets back the focus(calendar app is closed). Whitin the invokation of calendar it is possible, that there are changes made to the calendar events, so my PIMListListener(separate class) is fired.

Within that listener, a class method of my screen class is invoked, which have to be static.

And within that method , the code above should be called, which is not possible.

 

How can I do so?

 

cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Cannot use "this" in a static context

you can implement a listener anonymously

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700

Re: Cannot use "this" in a static context

I hope I understood:

 

i have to declare a listener while instantiating my datefield!?

but what kind of parameter should I use?

 

thanks.

cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Cannot use "this" in a static context

 


michaelk1 wrote:

 

Within that listener, a class method of my screen class is invoked, which have to be static.

 


 

Why does it "have to be static"? Listener methods (observer pattern) are usually called within an object (instance) because you register an object as a listener, not a class.

 

If you are stuck, tell what you are trying to achieve and we might help here...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Cannot use "this" in a static context

http://leepoint.net/notes-java/GUI/events/anonymous_listener.html

 

this is a basic principle of java, or rather of programming languages in general.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700

Re: Cannot use "this" in a static context

hi arkadyz,

 

within my screen class, I have a method called "setfromcod", its parameter is "int status" and in the body "globalclassvar = status".

I have a second class called "cod", in the body of this class i call setfromcod via "myscreenclass.setfromcod".

To do so, setfromcod has to be public static.

But I try to use a setchangelistener on a date object in the bode of setfromcod, which is not possible to do so.

 

thanks in advance

cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Cannot use "this" in a static context

Hi, michaelk1,

you are trying to tell me the details of the implementation. However, each and every piece of code is trying to achieve something (when I press this button, I want to see that screen; when my download completes, I want the screen to be updated; I want to have an accurate count of <the instances of this class> at all times; etc.) That "something" is what you need to realize.

 

Once you have that, the solution should become obvious.  If it doesn't, tell us that "something" and we will try to figure out the way.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 172
Registered: ‎04-19-2010
My Device: Bold 9700

Re: Cannot use "this" in a static context

Ok,

 

1. my application has one screen.

2. there is a vertical field manager which holds an objectlist

3. the objectlist is consisting of the event items from the present day

4. clicking at an event will call the bbs calendar app via invoke

5. now i want to make a refresh of the list, if something has changed

6. I want to do so via PIMListListener, which calles(if fired) a method "setfromcod" of my screenclass.

7. In that method the refresh method for my list will be called

8. in the refresh method there is the call of the setfieldchangelistener of the datefield i try to use.

 

 

cu
michael
--------------------------------------------------------------------------------------------------------------------------
please give kudos to thank everyone who helped you or put you in the right direction.
please mark the thread as solved, if you found your solution.