Do C# and java does support static types?
I have been using Singleton classes and static method for a while and always used to wonder how nice it would have been to have a seperate type which is a sta开发者_开发技巧tic type and cannot be instantiated but have only static methods!
It will be easy for readability and also to maintain.
for Ex
public UtilType MyUtility
{
public void Calculate(int x,int y)
{
return x+y;
}
}
Here MyUtility
should not be allowed to be instantiated only its methods can be accessed in static way.
In C# you can do that by using the static
keyword:
public static class MyUtility
{
public static void Calculate(int x,int y)
{
return x+y;
}
}
That would allow you to write
var result = MyUtility.Calculate(1, 2);
all over your code.
However, it is not very object-oriented, so always consider if there are better alternatives. Object-oriented design is about data and behavior, and such a static class is pure behavior. That's almost as bad as classes that are pure data, but those are often seen together.
I personally find overuse of static types a code smell.
Have you tried... static types?
public static class MyUtility
{
public static void Calculate(int x,int y)
{
return x+y;
}
}
and...
public class AnyOtherClass
{
public void AnyOtherMethod()
{
MyUtility.Calculate(4,5);
}
}
Refining the answer posted by masher
public final class Utilities
{
// private constructor
private Utilities(){ }
public static int add(int x, int y)
{
return x+y;
}
public static int subtract(int x, int y)
{
return x-y;
}
}
Why C# and java does not support util type ?
To answer your question, we cannot read the minds of the designers of C# and Java, so we can only infer their reasons for not supporting 'utility' classes. But the reasons probably include:
Utility classes are not object oriented, and are infrequently used in well designed OO application.
They are semantically redundant. You can code a normal class so that it cannot be instantiated, which is the only essentially different thing about a utility class.
The coding effort to turn a regular class into a "utility" class is trivial. In Java for example it is one line of code; i.e. a private no-arg constructor.
Since utility classes are semantically redundant, infrequently used, and trivial to code in Java / C# as they are currently specified, there is no real case to add syntactic sugar to the language to support them.
And even if it were a good idea, the down-sides of making such a change would include:
The considerable cost of revising language specifications, modifying compilers and associated tools, revising / extending tutorials and text books, and so on.
The impact on the existing Java / C# customer application codebase of adding a new keyword.
The extra increment of difficulty in learning the language caused by adding a new class variant.
How about declaring a private constructor?
public class MyUtility
{
private MyUtility ( )
{
}
public static int calculate(int x,int y)
{
return x+y;
}
}
With Java 5+, you can even use static import.
...
import static MyUtility.calculate
...
void myMethod ( )
{
int sum = calculate( 1, 2 );
}
Why doesn't this work? (in Java)
public class Utilities
{
private Utilities() {} //added in an edit after reading comments
public static int add(int x, int y)
{
return x+y;
}
public static int subtract(int x, int y)
{
return x-y;
}
}
(Now) You can't instantiate it, it only has static methods...
Instead of creating a private constructor you can use an enum type (which has a private constructor by default.
public enum Utilities {
;
public static int add(int x, int y) {
return x+y;
}
public static int subtract(int x, int y) {
return x-y;
}
}
Note: you need a lone semi-colon to indicate there are are no values of the enum.
I think an answer to the original question is that both languages are Object-oriented by definition. So, objects are first-class citizens i.e. everything is an object.
That's how the language was designed, how it's intended to be used and how the compiler figures out how to link parts of code to create a program.
static
helps make our lives easier, but sometimes we have to pay for that. for example, when you wish you could override a static method.
精彩评论