Win a copy of Event Streams in Action this week in the Java in General forum!

Dennis Schroeder

Greenhorn
+ Follow
since Aug 31, 2015
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dennis Schroeder

Hello,
I have the following (simplified example code):

This works so far.
Now I'd like to access above paint() method also in another (utility) class, to draw various things on screen, is this possible?

edit:
I have it working, I think:

Is this the corect (only) way to do it?
What concerns me a bit is that all public / proteced members members are now also in this class, but I actually don't need it there, I only want to access the paint() method from the other class..

edit2:
Solved.
I figured the simplest way is to pass g in the method calls, than I can acces it directly in my utility class.


Thanks for clarifying, I originally thought J2ME was the more recent version (that '2' tricked me).
3 years ago
Yes,

"Beginning Java ME Platform"
I have this one, I find it nicely understandably written.

and (J2ME focused)
"J2ME in a nutshell"



3 years ago

Knute Snortum wrote:
If you're dead set on doing it your way, you could always to this:



I just tried it now and that's what I was looking for.
So with this code I now get a bookmark that looks like this, as I wanted.



Actually quite simple solution but I somehow didn't think of doing it this way, guess I got confused.
If this way of doing it works in context of the whole project remains to be seen though when I get further.
Anyways, original question is solved, big thanks again.

Can / should I somehow mark this thread as resolved ?
edit: I just noticed the 'Resolved' button at the top and clicked it.


3 years ago
Thanks for reminding me that it's about references, I actually know but being new to Java (and quite new to coding in general) I tend to forget about it.
So yes, in this light, I see your point, thanks.


3 years ago

Knute Snortum wrote:

So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.


I don't think this is correct. If you need the values of a Player object, storing its reference is the way to do it.



Could of course be that I'm thinking wrongly here but I'll try to clarify:

Scenario:
AudioPlayer is running on the cellphone, playing a track from a playlist.
Before I quit the player I save a bookmark. Bookmarks contains current track, current playlist etc.

I quit the player, restart it, a new (default) player instance is created.
To restore the player in its previous state (to resume playback where it was when saving the bookmark) I'll just need to load the values saved in the bookmark like current track etc.
I don't need the saved player instance for it. at least I wouldn't think so, couldn't try in practice yet.

This is what I currently have, running the debugger:



So bookmark is created fine with the desired values, only that I think I actually don't need the PlayerCanvas instance (the player itself).

Anyway, your code suggestion seems to be the ticket (if I should be right ), will try how it goes and report back.
Thanks.

edit:
I just thought about this, maybe this comparison is totally off..
To store a bookmark say in a web browser, you just store certain data (like page URL), not the whole browser instance.
Uhm..does this make any sense
edit2:
After reading the post below, I notice I missed the difference between object copies and references.




3 years ago

Carey Brown wrote:Your Bookmark class should not have a getBookmark() method because it already is a Bookmark.



Sorry, should have mentioned this is simplified code from another class, not in the Bookmark class.
As said, it basically works already currently to get the bookmarks, except that the Bookmark object also contains the player which I'd like to avoid.


You will need getters for the fields (except for player). You should probably not have setters for the fields. It is possible you might want a play(AudioPlayer player) method to return to a bookmark. Other than that it's a bit hard to tell without the API for AudioPlayer.



Ah yes, sounds like I can solve it with getters for the fields, leaving the player out, I didn't think about this.
Thanks.

3 years ago

Paul Clapham wrote:

Dennis Schroeder wrote:But - the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player (see comments in code).



I don't understand why you make this requirement. And the comments in the code didn't enlighten me in any way. So could you explain in more detail why you say that?




Thanks for the reply.

Sorry, yes, I should have explained in more detail.
This is an audio player for a cellphone (pre-smartphone area), the bookmarks get stored in the permanent storage of the player.

So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.

I hope it's more clear now.
3 years ago
I'm currently programming a 'Bookmarks' function for an audio player.
A Bookmark object holds current data of the player like current song, elapsed time of the song etc for resuming later.

I got it almost working as it should, but here's the problem:
I need an instance of the player for accessing the current values, so I've put it in the constructor of my Bookmark class.
But - the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player (see comments in code).

How can I solve this ?

Things I thought about but not sure if / how it works:

- make player instance null after I recieved the values (seems hackish)
- make an overloaded constructor somehow which doesn't take a player instance
. other ways to (temporarily) access the player object

Thanks.



3 years ago
Thanks for the replies.

edit:
I knew that the common way of access is without an object, so I was just a little surprised initially that access via object also works.

Understood and marked as solved.






3 years ago
I was surprised that this compiles without errors:



Why am I allowed to access static methods and variables in a non-static way (via an object), as I thought the purpose of 'static' keyword is to serve as class variables / methods (object independant) ?
Is there a practical application for this ?

Thanks.
3 years ago
Henry Wong,

you're right. C++ also has the 'this' keyword with similar (same ?) functionality as in Java, it's just that in the C++ book I was learning from it was never used in the context of object variables.

So my question was more wondering if the way I already knew from C++ can by applied the same in Java or if there's any difference, rather than comparing the this keyword itself in both languages.

Hope this clears it up.

3 years ago
Thanks.

Your above code would give a name clash, i.e. it would assign the local variable number to the local variable number (and not to the object variable as intended), and so does nothing, correct ?
3 years ago
In Java I commonly see this:




From C++ I'm used to write this:



Are these different in functionality (I'd assume not), is one recommended over the other ?
3 years ago