I was beating my head, trying to understand why it needed a char or int. I even was trying it as:
That was throwing an error as well. I'm not sure if this can really be answered, but I'm not understanding why there wouldn't be an instance method. I'm also failing to understand how I could have determined that I needed to use:
Neko Singh wrote:I'm also failing to understand how I could have determined that I needed to use:
Because the API says the method is static. That means you should use the class name to call the method, not an instance.
Rob, thanks again for taking your time with me.
I was discussing this yesterday with a co-worker and I was wondering if you could answer a question for me.
Besides it being an overall design of Java, I was wondering if making Methods static also saved in memory usage. If these methods were not static, and were instead instantiated with each instance, would the objects then take up more memory?
I feel like this is a "no duh" conclusion, but I just wanted confirmation. =)
Static methods do not require an instance to be called. So if you only needed an instance to call one method that does not depend on the instance's state, then yes, you will save memory by using a static method.
Let's take a modified version your example, and assume that auto boxing does not use cached values*.
This code creates object "grade"** only to call the method. This method does not use any internal fields of the instance, and afterwards the instance is ignored. So that instance is created needlessly, and takes up unnecessary memory***.
* It does though, for all values between 0 and 127 (int values), a.k.a. those that are in the upper table at http://www.asciitable.com.
** OK, in real life it doesn't because of the caching of 'C' which is below 127.
*** You probably already figured out this instance is created anyway because of the caching.
I see what you mean. You're discussing the difference between my code needlessly using Character, rather than using char. Creating the object "grade" would take up more memory than creating a char. Correct?
I was probably asking a silly question. I was asking about the overall design aspect of Java in its use of static methods vs instance methods.
Essentially, would an object instance based on a class with static methods take up less memory than if Java design didn't include the existance of "static methods" and had all methods be instance based?
This was the "no duh" question I was referring to.
You're being very kind in answering my questions. I feel my questions are foolish.
Neko Singh wrote:Essentially, would an object instance based on a class with static methods take up less memory than if Java design didn't include the existance of "static methods" and had all methods be instance based?
No. An object instance doesn't contain any methods when it's stored in memory. It doesn't contain static methods or instance methods either.
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad: