|
Ensign Expendable posted:I have never heard of instanceof being a bad idea. You could do something like I would prefer to do this, but since my instrument classes are extending JPanel already I can't give them another superclass. I know that I could have the superclass itself extend JPanel and then by inheritance the subclasses would also become JPanels, but I'm pretty new to Java and I'm designing the JPanel in Netbeans, so if my instrument classes don't directly extend JPanel then I'd have to do the swing layout entirely in code which I'm not prepared to do. Unless anyone knows a way to do what I'm describing in Netbeans? The closest I can think of is to design the JPanel, then copy and paste the component initialization into the instrument subclasses.
|
# ? Aug 30, 2011 22:20 |
|
|
# ? May 8, 2024 10:51 |
|
Write an interface Instrument that defines getSettings() and other stuff Instruments need.
|
# ? Aug 30, 2011 22:28 |
|
Ensign Expendable posted:Write an interface Instrument that defines getSettings() and other stuff Instruments need. Ok I just did that and it seems to work. I didn't think interfaces worked like that. Thanks everyone!
|
# ? Aug 30, 2011 22:29 |
|
HFX posted:Using it for the method you describe cries that you should implement an interface that Instrument classes subclass to provide that method. It is considered bad form when you are using it to basically serve as the old dreaded C 200 line if..else if.. statement. Basically, used too liberally it can point to issues with design. You're right that its not always inappropriate, but I'd much rather people default to thinking it bad than not. The cases where it is used inappropriately (i.e., they created a poor design and aren't willing to fix it) seem to be far more widespread than appropriate uses. Luminous fucked around with this message at 17:56 on Aug 31, 2011 |
# ? Aug 31, 2011 17:53 |
|
Anyone any tips on the easiest/best method for utilising properties files to store system messages? I want to move all my "Error parsing comms" etc messages to a properties file, so that the system/API can just import com.x.pz.messages and then use the constants held within (that have been populated from the properties file). Ideally I'd like the properties file to look like: code:
code:
Once I've imported com.x.pz.messages I'd like to then just substitute them in as you'd expect: code:
code:
code:
iam fucked around with this message at 09:00 on Sep 1, 2011 |
# ? Sep 1, 2011 08:34 |
|
iam posted:Anyone any tips on the easiest/best method for utilising properties files to store system messages? Well at a previous place we had a wrapper called CachedTypedProperties code here https://gist.github.com/1185736 Theresa
|
# ? Sep 1, 2011 09:47 |
|
In order to solve this problem, you're going to have to do one of two things: 1. Have a line of code somewhere saying that there is a property called <whatever>, or 2. Forgo compile-time checking, and have some way to map strings to messages (and use that every time you access a message). If you do go for (1), then you don't have to do that in every file - in fact, you could automatically generate a file that looks like so: code:
Or don't do a static import, and just access stuff as ErrorStrings.COMMS_ERROR or whatever.
|
# ? Sep 1, 2011 10:02 |
|
I'm just getting started learning Java, and when the time came to learn about applets, I hit a snag. I'm using Eclipse, and I've got a bare-bones do-nothing applet written as a test. It compiles and runs in Eclipse, but it throws an exception when I try to use it in an .html with Firefox. Using Windows XP, if that matters. Here is the code I'm using (just taken from some tutorial): code:
code:
And here's the console barf: code:
code:
Eclipse and Firefox/Plug-in are both using the same JRE version: 1.6.0_27 Have mercy on this stupid goon who just wants his dumb applet to work.
|
# ? Sep 1, 2011 17:34 |
|
King Lazer posted:
Move the AppletTest.class file to a directory called learning in the same location as the class file is currently.
|
# ? Sep 1, 2011 18:09 |
|
baquerd posted:Move the AppletTest.class file to a directory called learning in the same location as the class file is currently. originally both files were in C:\Program Files\Java\eclipse\workspace\Learning\bin\learning Moving the .html up into bin has worked. Let the name baquerd ring from the mountaintops! Thanks!
|
# ? Sep 1, 2011 18:25 |
|
Is there a Java equivalent of easy_install and eggs from python? I'd really like to be able to do something like:code:
|
# ? Sep 1, 2011 21:54 |
|
wins32767 posted:Is there a Java equivalent of easy_install and eggs from python? I'd really like to be able to do something like: Apache ant is the classic choice, though there are a lot of utilities you can do this with.
|
# ? Sep 1, 2011 21:58 |
|
Why does transfer_wrong1 have a race condition?code:
|
# ? Sep 1, 2011 22:45 |
|
wins32767 posted:Is there a Java equivalent of easy_install and eggs from python? I'd really like to be able to do something like: Sounds like you want Maven
|
# ? Sep 1, 2011 22:51 |
|
tractor fanatic posted:Why does transfer_wrong1 have a race condition? code:
transfer_wrong1(1) withdraws from account x, OutOfMoneyError thrown transfer_wrong1(0) deposits into account x -vs- transfer_wrong1(0) withdraws from account y transfer_wrong1(0) deposits into account x transfer_wrong1(1) withdraws from account y transfer_wrong1(1) deposits into account y There's other ways to race this but if the error is thrown that's the cleanest to see.
|
# ? Sep 1, 2011 23:08 |
|
tractor fanatic posted:Why does transfer_wrong1 have a race condition? e: but I think the dude above me probably knows more than I do also why does this seemingly money-related class use floats
|
# ? Sep 1, 2011 23:09 |
|
tractor fanatic posted:Why does transfer_wrong1 have a race condition? If you calculate the total balance of the accounts between the withdrawal and deposit, some money will have disappeared into the void.
|
# ? Sep 1, 2011 23:15 |
I run into things like this a lot:code:
|
|
# ? Sep 1, 2011 23:28 |
|
If the code is multithreaded, the condition may have changed. Also I guess it makes it a little more readable. Even if there are no external threads changing the condition, you still know when .doSomething() runs without having to scroll up to find out why myObject is or isn't null.
|
# ? Sep 1, 2011 23:44 |
|
fletcher posted:Instead of checking for someCondition again, you could do a if (myObject != null), and the code would function the same way. Is there a good reason to use one way over the other? If the conditions to initialize your object are complex, go with comparing against null so that the complexity is contained in one place. But really, you should avoid this situation all together, and initializing something to null before actually initializing it is a clear warning-sign that all's not well. Maybe you could structure the code in some other way: code:
|
# ? Sep 2, 2011 02:38 |
|
baquerd posted:
but if you are relying on one happening before the other you shouldn't be using concurrency anyways, right? I mean this race condition would appear no matter what solution you try since you can't control which transfer comes first.
|
# ? Sep 2, 2011 04:52 |
|
tractor fanatic posted:but if you are relying on one happening before the other you shouldn't be using concurrency anyways, right? Properly done concurrency either does not allow the transfers to occur non-atomically or implements a non-locking control that provides for ordered transactions. The general idea is to maintain a consistently valid read state. It's OK if the transfer fails as long as the state is not at any point readable in an incorrect manner by an outside observer. Here is a (bad, trivial, etc.) solution to the problem assuming all transactions run through a single JVM instance and this is the whole of the class definition: code:
baquerd fucked around with this message at 05:36 on Sep 2, 2011 |
# ? Sep 2, 2011 05:24 |
|
baquerd posted:Ah, I didn't think of using a static solution to solve this with locks. Anyways, I'm an idiot because the paper I found this in explains that the problem they were worried about is what Sedro was saying about the balances not summing up properly. I misinterpreted the "sum of balances" comment.
|
# ? Sep 2, 2011 05:45 |
|
If the only operations are transfer, withdraw, and deposit, then the original implementation is almost sufficiently atomic because of the specific constraints of the problem. The only issue is a self-transfer, where an interleaved withdrawal could fail in a way that can't happen in any serialization. But adding almost any other operation is likely to change that. Certainly a "current total in all accounts" would. And it's probably not worth carefully analyzing the set of operations to find an optimal synchronization strategy. Using a universal lock solves the deadlock problem, as does always grabbing multiple locks in a globally-determined order (e.g. in order of increasing account number).
|
# ? Sep 2, 2011 06:13 |
|
Sorry if this is really basic, it's been about 3-4 years since I've done any real Java and I don't know how to Google this because that's exactly what it's about : this. When is it appropriate and when is it inappropriate to reference a variable/function/anything as this.exampleThing instead of just exampleThing? No need to re-invent the wheel, if someone could point me at the correct search term I'd really appreciate it. So far, I've just found "it's a style thing" which is kind of insane. Why put "this" in front of every god drat thing if it's not needed? Edit: stackoverflow posted:You only need to use this - and most people only use it - when there's an overlapping local variable with the same name. (Setter methods, for example.) Really? Really? gotly fucked around with this message at 04:00 on Sep 3, 2011 |
# ? Sep 3, 2011 03:58 |
|
Yeah, the only time I've ever seen it is when a parameter and an instance variable have the same name, or an instance variable and a local variable. (edit: I guess a parameter is just a local variable, isn't it?) code:
|
# ? Sep 3, 2011 04:02 |
|
carry on then posted:Yeah, the only time I've ever seen it is when a parameter and an instance variable have the same name, or an instance variable and a local variable.
|
# ? Sep 3, 2011 04:17 |
|
carry on then posted:Yeah, the only time I've ever seen it is when a parameter and an instance variable have the same name, or an instance variable and a local variable. (edit: I guess a parameter is just a local variable, isn't it?) Just to make sure I have it 100% - in this case, MyClass will actually change BUTTS and leave CHEWBACCA alone?
|
# ? Sep 3, 2011 04:30 |
|
gotly posted:Just to make sure I have it 100% - in this case, MyClass will actually change BUTTS and leave CHEWBACCA alone? It assigns the value of CHEWBACCA to BUTTS.
|
# ? Sep 3, 2011 04:38 |
|
gotly posted:Just to make sure I have it 100% - in this case, MyClass will actually change BUTTS and leave CHEWBACCA alone? Yes, as Ranma says. Here's some "fun" code showing another way to use "this": code:
baquerd fucked around with this message at 05:24 on Sep 3, 2011 |
# ? Sep 3, 2011 05:15 |
|
I mostly use this (and base, super, etc) when I want to filter autocompletion. Naming conventions like _fieldName solve the naming conficts (safer to avoid them entirely). There are some cases where it makes stylistic sense: code:
|
# ? Sep 3, 2011 05:20 |
|
Thanks guys, cleared it up a lot. I program in something drastically different during the day so it's always nice to see what seasoned Java guys consider best practice. Also, if I could write production code like baquerd I'd die a happy man.
|
# ? Sep 3, 2011 05:31 |
|
Sedro posted:Naming conventions like _fieldName solve the naming conficts (safer to avoid them entirely). also never ever write a method named equals that takes in a specific kind of object and returns a boolean
|
# ? Sep 3, 2011 06:02 |
|
Do whatever you want with private fields as far as I'm concerned. I would like to hear your reasoning against the typed equals method though.
|
# ? Sep 3, 2011 07:06 |
|
Sedro posted:I would like to hear your reasoning against the typed equals method though. There are tons and tons of non-obvious pitfalls around writing a correct equals method. Here's what looks like a perfectly normal implementation of some equal butts if you don't think too hard about it, but it's very wrong - there are several different issues with this code: code:
vvvvvvvvvvvv drinking heavily and not testing poo poo is the best way to post brosmike fucked around with this message at 11:38 on Sep 3, 2011 |
# ? Sep 3, 2011 09:49 |
|
Sedro posted:I would like to hear your reasoning against the typed equals method though. Joshua Bloch, Effective Java posted:It is acceptable to provide such a “strongly typed” equals method in addition to the normal one as long as the two methods return the same result, but there is no compelling reason to do so. It may provide minor performance gains under certain circumstances, but it isn’t worth the added complexity (Item 55). e: Except in the above article, replace the entirety of Pitfall #3 with "Don't mutate objects being used as keys in a Map (or present in a Set)." Malloc Voidstar fucked around with this message at 10:32 on Sep 3, 2011 |
# ? Sep 3, 2011 10:16 |
|
brosmike posted:
I get false, false, true. Overload resolution is done based on the static type of the expression - since Object doesn't have an equals(Butt) method, equals(Object) gets picked. The fact that you're not actually overriding the equals method is begging for trouble, but it's not quite as bad as you're presenting.
|
# ? Sep 3, 2011 11:13 |
|
I would never write a typed equals without overriding object.equals and object.getHashCode. When I see a typed equals I immediately know that the type implements logical equality without looking at comments/source.
|
# ? Sep 3, 2011 16:31 |
|
Sedro posted:I mostly use this (and base, super, etc) when I want to filter autocompletion. Naming conventions like _fieldName solve the naming conficts (safer to avoid them entirely). I think there is plenty of semantic clarity to 'this.field', and it is an explicit meaning that is enforced by the language itself. I always prefer using language constructs over implicit conventions that may be arbitrary, conflicting, forgotten by the time you have to maintain it, or not communicated to the next programmer (internal or external) who has to look at the code and may have different assumptions about what makes for an intuitive name.
|
# ? Sep 3, 2011 16:51 |
|
|
# ? May 8, 2024 10:51 |
|
Aleksei Vasiliev posted:those conventions are poo poo and make code look terrible, why people don't just use this and avoid polluting their variables I will never understand
|
# ? Sep 3, 2011 17:14 |