File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Memory related query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory related query" Watch "Memory related query" New topic
Author

Memory related query

Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Hi Folks,

I have a class with the member methods are static(not member data).
I have written this class to hold the utility methods for my application.But i have declared all the member methods are static.

Can anyone please confirm me whether the memory (stack) gets allocated to functions as soon as i run my application or the memory will be allocated based on the normal function call.

I wanted to know is this the write approach to code or not ?

Thanks & Regards
Divakara
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Divakara Sr wrote:I have a class with the member methods are static(not member data).
I have written this class to hold the utility methods for my application.But i have declared all the member methods are static.
Can anyone please confirm me whether the memory (stack) gets allocated to functions as soon as i run my application or the memory will be allocated based on the normal function call.

As I understand it: the latter; however, you really shouldn't be concerning yourself about this at this stage. Many Java programmers (myself included) go for years without knowing exactly how the JVM "does its stuff"; and you should never design your programs based on how you think things might be done.

I wanted to know is this the write approach to code or not ?

Without the actual code, it's difficult to say; but it is a fairly widely-used approach for genuine utility methods. The question is: how "genuine" are yours?

One other thing you might want to think about is whether these methods can be split up in some logical way. A single 'Utils' class for an application that contains a thousand methods is probably bad design.

My rule of thumb: when you're writing a method, think first whether it belongs in a class (and don't forget that regular classes can have static methods). Only put it in a utilities class when you're absolutely sure that it doesn't belong somewhere else.

Winston

[Edit:] Almost forgot: The first thing you should do when writing a method (particularly a utility one) is to find out whether somebody else has already written it.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Hi Winston,

Thanks for the reply.

Yes I have written only generic functions means the functions which are used by most of the classes in my application.
I have put all generic functions in one class and made all functions static to avoid creation of objects(Its just my mind).

Can i go with this approach ?

Thanks & Regards
Divakara
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Divakara Sr wrote:Yes I have written only generic functions means the functions which are used by most of the classes in my application.
I have put all generic functions in one class and made all functions static to avoid creation of objects(Its just my mind).

As I sad: without seeing the code (or some of it - perhaps the first 5 methods - if the class is huge), it's very difficult to tell.

Also, how many methods do you have in this class? (See my comment in the previous post)

Winston
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Ten methods i have written and no one had written those methods. that is very specifically to my application and generic for all the classes which are in my application.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2268
    
    3

I myself try to avoid static methods as much as possible. If we think well, most of the time, there is always a good object where these methods would fit well. And there, they would be instance members.

Another point, we always have to take care with static methods because this can be a good sign the the program is procedural.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Divakara Sr
Greenhorn

Joined: Sep 06, 2011
Posts: 8

Roberto Perillo wrote:I myself try to avoid static methods as much as possible. If we think well, most of the time, there is always a good object where these methods would fit well. And there, they would be instance members.

Another point, we always have to take care with static methods because this can be a good sign the the program is procedural.



Thanks, May i know the conclusion ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Divakara Sr wrote:Thanks, May i know the conclusion ?

I'll try one more time: Without seeing your code, it is very difficult to tell.

There is nothing intrinsically wrong with what you're doing, but you may be making bad choices in this case. Show us the code, and we can answer your question.

Winston
 
Don't get me started about those stupid light bulbs.
 
subject: Memory related query