This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to use static method

 
Dora Di Donato
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  •  
    Mahtab Alam
    Ranch Hand
    Posts: 391
    1
    Java MySQL Database PHP
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 9478
    50
    Eclipse IDE Hibernate Ubuntu
    • 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Dora Di Donato
    Ranch Hand
    Posts: 85
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you :-)
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic