Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory related query

 
Divakara Sr
Greenhorn
Posts: 8
Android Fedora Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Divakara Sr
Greenhorn
Posts: 8
Android Fedora Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Android Fedora Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Divakara Sr
Greenhorn
Posts: 8
Android Fedora Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic