Shawn Ma wrote:I have the following example code, in which I don't understand the block in braces underlined. Can anyone explain to me the role of it(constructor? private method??..)
Is there any better way to write the code?
also can you give me any comment of my code? (including the namings)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
It works roughly the same way as a parameterless constructor, and is run after the super() call in the constructor, but before the rest of the constructor body. They are rarely useful, since you can do most of the stuff they do in a constructor instead.
You should also check at the start of a method whether the arguments are in agreement with the values that are allowed.
Right now I can't tell from a glance what your methods createIdentityOpertor(), identitize() and identity() are supposed to do, and how they are different (although identity() seems to be an alias for createIdentityOpertor()... why?). P.S. shouldn't that read Operator instead of Opertor?
Stephan van Hulst wrote: You could use a Deque<Matrix> instead. ArrayDeque and LinkedList already provide a full implementation.
Winston Gutkowski wrote:
What you'll see more often are static initializers, which is basically the same construct, except with the word static in front of the opening brace; and they're used for the same sort of thing as you have here: a variable that can't be initialized in a single statement (except that static initializers work with static variables).
Yes: put the code in the constructor. To be honest, I've never found a need for a non-static initializer; although I imagine it might be worth doing if you would otherwise have to repeat the code for a whole bunch of constructors (which doesn't appear to be the case here).
Well, identitize is an odd name for a method.
When you have an instance variable of any reference type, including arrays, it will start out as null. When you assign a new array to it, you will have allocated memory to hold pointers to all its elements, and initially all these pointers are null. If you want every element to be non-null, you have to manually create a new element at every index.
No, that is mistaken. The instance fields are implicitly initialised to 0, false, or null when the class is initialised. Then the instance initialisers are called, then the constructors. So those variables exist before the constructor. The full details are in the Java Language Specification.Shawn Ma wrote: . . . my understanding is:
Instance variables always exist and starts from a constructor, . . .
Shawn Ma wrote:my understanding is:
Instance variables always exist and starts from a constructor, so (not static)initializer can always be replaced by a constructor.
Static(class) variables can exist without a constructor, so the initialization may not be realized by a constructor. When it can't be put on one line, we need an initializer block. Because it's static variable, the initializer is called "static initializer".
From the oracle websiteswebpage, it says "There is an alternative to static blocks — you can write a private static method". Now I don't quite understand the "private static method":
1. "static": why is it static? is it that method using only static variables are defined as static method, because the method can be called without creating an instance of the class?
2. "private": private means not accessible from outside of the defining class. so static variable initialization should be prohibited from outside of the defining class?
On the same oracle page, when compared this alternative with that for non-static initializers: "protected final method", I don't understand why one is "private" not "protected", not "final", and the other is?
Sorry this is not quite related to my original question, but I still want to take this chance to learn the basics.
No, of course they don’t. You have two sorts of array there; the array of primitives is implicitly filled with 0s, and the Object[] with nulls.Shawn Ma wrote: . . .
private Matrix[] stackMatrix = new Matrix[MATRIX_STACK_SIZE];
private int[] stackMatrix = new int[MATRIX_STACK_SIZE];
Shawn Ma wrote:so without further initialization, the following two statements have the same effect?
private Matrix[] stackMatrix = new Matrix[MATRIX_STACK_SIZE];
private int[] stackMatrix = new int[MATRIX_STACK_SIZE];
Stephan van Hulst wrote:Personally, I don't like using all-caps for constants. It makes my code look very angry. I like to use the same conventions for them as regular variables. But again, this is a personal choice for me.
Campbell Ritchie wrote:
No, of course they don’t. You have two sorts of array there; the array of primitives is implicitly filled with 0s, and the Object[] with nulls.Shawn Ma wrote: . . .
private Matrix[] stackMatrix = new Matrix[MATRIX_STACK_SIZE];
private int[] stackMatrix = new int[MATRIX_STACK_SIZE];
Bear Bibeault wrote:I find it odd that you admonish the poster for not using proper conventions for class naming, and then suggest violating the conventions for constant names. I'd recommend following the conventions, even if it makes for "angry" constant names.
Mike Simmons wrote:And nowadays you also see all caps for enum values - often, but not always. Which are usually immutable, but not always.
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|