• 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 a Class constructor be Static

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have attended java Programmer test in this
site. One of the question in the exam is
Question : Can a Constructor of a class be static
Answer : No
I am not cleared with the explanation given, because i have studied about FACTORY CLASS, which are static methods return the objects of same class type.
please clear my doubt.
thanks in advance
Sincerely,
mvksk.
Thanks in advance
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

constructor cannot be native,abstract,final,synchronized,static
according to javac
--Does FACTORY CLASS do the job by constructors??
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A factory method can be static, but it isn't a "constructor". Ok, it constructs objects, but "constructor" refers to a special kind of method in a class that is for creating an instance of that class. It must have the same name as the class and not return anything (not even void). An official constructor cannot be declared static.
A factory method (as in the Facotry Pattern (GoF)) will return different objects depending on what data it receives. But it is not an official "constructor", it is a method - which can be static.
I hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic