• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can static method return instances of the same class in the special case where everything is static?  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can static method return instances of the same class in the special case where everything is static?


Why not? Did you try it?

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have utility classes, which have all their members static, and never therefore need any instances. Look at the Math class, for example. There is no need ever to have an instance of such classes, so they usually prevent instantiation by the method shown in the Java Language Specification. Try writing Math m = new Math(); and the compiler will complain bitterly because you can't instantiate Math.

There are classes like this, which was mentioned in a recent thread. If you look at it, you find it has several factory methods, which are static methods called getXXXInstance. Factory methods are called on the class in order to get an instance. Although BreakIterator itself is abstract, it has several subclasses, presumably as private inner classes or package‑private top‑level classes in the java.text. package, where us mere mortals can't find them. In that case, you can subclass BreakIterator, but for most uses, you simply call a getXXXInstance method and it provides an instance of itself all ready for the task in hand. You can even have classes with private constructors and factory methods which provide all the instances the classes's creators thought you would use. Note that most of the BreakIterator methods are not static, but you have to get an instance in order to use them.

I hope this goes some way towards answering your question.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can have utility classes, which have all their members static, and never therefore need any instances. Look at the Math class, for example. There is no need ever to have an instance of such classes, so they usually prevent instantiation by the method shown in the Java Language Specification. Try writing Math m = new Math(); and the compiler will complain bitterly because you can't instantiate Math.

There are classes like this, which was mentioned in a recent thread. If you look at it, you find it has several factory methods, which are static methods called getXXXInstance. Factory methods are called on the class in order to get an instance. Although BreakIterator itself is abstract, it has several subclasses, presumably as private inner classes or package‑private top‑level classes in the java.text. package, where us mere mortals can't find them. In that case, you can subclass BreakIterator, but for most uses, you simply call a getXXXInstance method and it provides an instance of itself all ready for the task in hand. You can even have classes with private constructors and factory methods which provide all the instances the classes's creators thought you would use. Note that most of the BreakIterator methods are not static, but you have to get an instance in order to use them.

I hope this goes some way towards answering your question.


Thanks for clearing that out , i know that some classes are fully static and cant be instanced (usually by making the constructor private) but i had issues understanding why you would make a static method return a instance of the same class the method is in (dont you have a constructor for that) But it can work . Correct me if i m wrong as i define a static method , a static method is a method that does not need the instancing of the class to work and it must not contain any instance variable and instance methods .
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote: . . . some classes are fully static . . .
Don't call that a static class. The only classes you can call static would be nested classes.
. . . static method is a method that does not need the instancing of the class to work and it must not contain any instance variable and instance methods .
You have done it now You have forced me to quote the Campbell Ritchie Classification of Methods.

Or, yes.

And, “you're welcome ”.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:
Thanks for clearing that out , i know that some classes are fully static and cant be instanced (usually by making the constructor private) but i had issues understanding why you would make a static method return a instance of the same class the method is in (dont you have a constructor for that) But it can work . Correct me if i m wrong as i define a static method , a static method is a method that does not need the instancing of the class to work and it must not contain any instance variable and instance methods .


What you have issues understanding is actually a classic design pattern ... http://en.wikipedia.org/wiki/Factory_method_pattern. With this pattern, the constructors are private, and you have to use static methods to instantiate (and configure) objects.

Henry
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i had issues understanding why you would make a static method return a instance of the same class the method is in (dont you have a constructor for that)

I can give an example from Generics. Before diamond operator, evey time you would instantiate a generic class it went like this:

The type argument from the right-hand is redundant (we already wrote it). To walkaround this you can write a static generic method to return an instance.

Now you simply:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!