File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes to use static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "to use static method " Watch "to use static method " New topic
Author

to use static method

Dora Di Donato
Ranch Hand

Joined: Nov 27, 2009
Posts: 85
Hi guys,
I know the constructor is mandatory and there us always one the default but i can use a static method instead it?

Thanks
Di Donato
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

I'm not sure what you are asking, but I think it is one of these:

  • You can create a class with all static methods.
  • You can create a class with all static methods and make the constructor private so callers can't call it.
  • You can create a class that has a static method that calls the constructor rather than the caller doing so


  • [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Mahtab Alam
    Ranch Hand

    Joined: Mar 28, 2012
    Posts: 281

    Its better to use constructors for initialization of objects fields.
    Because when you use static method it means that code inside that static method will behave the same for all the objects


    So if I have a Student class and a constructor in that class that take (String name,int age,char gender)
    I can create and initialize object as Student stu=new Student("Mahtab Alam",22,'M'); in one line

    I don`t see static methods doing any meaningful initialization of object`s fields.

    Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 8008
        
      22

    Mahtab Alam wrote:Its better to use constructors for initialization of objects fields.
    Because when you use static method it means that code inside that static method will behave the same for all the objects

    Which may be precisely what you want.

    Static "constructors" (usually called 'factory methods') can often be a better alternative than actual constructors, particularly for immutable objects. Just a few things being:
  • You can give them meaningful names. Arguably, Java could have done that with the String class, rather than force us to learn the 15 different ways to create a String (two of which, I notice, are now deprecated).
  • You can return a subtype (possibly private or anonymous) of the class that includes them.
  • You can control the instances you create. Integer.valueOf(int) is a very good example of this, since it will only return a new object if the supplied value is outside the range -128 to +127.
  • You can hide your constructors (and "hiding" is generally a good thing ).


  • HIH

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Dora Di Donato
    Ranch Hand

    Joined: Nov 27, 2009
    Posts: 85
    Thank you :-)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: to use static method