• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
reply
    Bookmark Topic Watch Topic
  • New Topic