aspose file tools*
The moose likes Beginning Java and the fly likes Clarification of underlying code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Clarification of underlying code." Watch "Clarification of underlying code." New topic
Author

Clarification of underlying code.

Nick Delauney
Ranch Hand

Joined: Sep 28, 2002
Posts: 43
Hello all,
My Question is in regards to what is happening when there is a new instantiation in a static method. The object is not created when the class is referenced, is it ?
example
class StaticUtil
{
public static aMethod()
{
ArrayList al = new ArrayList();
// some code
}
}
Two more Questions if you can answer.
2) Should all methods of a utility class be static ?
3) Is it true interfaces cannot have static methods, why not ?
If there to many questions, please answer any one you can.
Thanks for any assistance.


N.D:"Anything worth having, takes time to get"
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by Nick Delauney:
Hello all,
My Question is in regards to what is happening when there is a new instantiation in a static method. The object is not created when the class is referenced, is it ?
example
class StaticUtil
{
public static aMethod()
{
ArrayList al = new ArrayList();
// some code
}
}

The ArrayList will not be created until the StaticUtil.aMethod() is called. static only means that this method does not need to be invoked on an object of the class. Note that multiple calls to your aMethod() will create multiple ArrayLists. The static nature of the method does not mean there is only one 'al'.
2) Should all methods of a utility class be static ?

I'm not sure what kind of utility class you're thinking about, an example might help. But, in general, I prefer to not have just static methods in a class. I think that having instantiable objects is preferable to calling static methods on a class. Since I'm not that familiar with the Java class library, there may be an idiom for certain cases. I'd like to see how other people respond to this question.
3) Is it true interfaces cannot have static methods, why not ?

Interfaces represent abstract functionality that must be implemented by the classes that implement that functionality. Static methods can't be overriden, so they can't be abstract [and all methods in an interface are abstract].


Richard
N 37 33 W 122 18
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
>Should all methods of a utility class be static ?
good question. if by utility class you mean "general purpose/public service" sort of class (like java.lang.Math) then yes - there are good reasons to disallow instance creation with the final keyword and make all the methods static. but if you mean something more general by "utility" then it may be that you want some polymorphic action out of yr class(es), then static methods, which belong to the class, are no good to you.


SJCP 1.2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification of underlying code.