• 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

"JavaRanch Rule Round-up Game" error?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question #86 "Can a constructor be declared private?"

The game says that the answer is No, but:

public class StrTest
{
public StrTest()
{
//super();
}
private StrTest(String one){ }

public static void main(String[] args)
{

StrTest rv1= new StrTest();

}


}

compiles with no warnings and runs.


(and it is even cocky enough to tell me: "So how do you make an object...?[..]". )
:-) Is it wrong or me?
[ May 19, 2008: Message edited by: Kosta Servis ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes a constructor can be made private.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well whatever you find at JavaRanch, was absolutely correct. if you put one arg constructor then compiler by default doesn't put any non arg constructor and first line inside a constructor can be a call to this or super constructor. if you don't put any,then by default is super(), the code you pasted is equivalent to this code:

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first a private constructor may sound silly. Google 'Singleton' if you want to find out more
reply
    Bookmark Topic Watch Topic
  • New Topic