• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors  RSS feed

 
Sriram Shanker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a test and i see this question in the list of previously asked questions-
Which of the following is valid (constructor) for the class Test:

1) void Test(){}
2) private Test(){}
3) public Test(){}
4) Test(){}
5) Test(int){}

Now i know that options (4) and (5) are valid but not too sure about the rest. Can someone please take the time to clarify my doubt?
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sriram,


1) void Test(){} - Is not a constructor, but will be treated as a method
2) private Test(){} - is a valid constructor and is in fact is one of the prequisite for Singleton Pattern. If you don't know about Singleton Pattern, if you can use this constructor to create instance of the class from class itself. Private entities is available to its own class
3) public Test(){} - is a valid constructor
4) Test(){} - is a valid constructor
5) Test(int){} - is a valid constructor


Why don't you write a small Test program and check out your self.
 
Sriram Shanker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rewa.
 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can #5 be valid? You need a parameter name!

Test(int i){}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fahd Shariff:
How can #5 be valid? You need a parameter name!

Test(int i){}


Is not "i" a parameter name?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think Fahd is saying that in the original post, #5 is
Test(int){}

and is not valid.

Fahd was then posting what WOULD be a valid constructor.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private Test(){} - is a valid constructor and is in fact is one of the prequisite for Singleton Pattern.


Just a minor correction ... the private constructor is one implementation of the Singleton pattern in Java. It's not the only possible implementation in Java, and the pattern has different implementations in other languages.

Just for grins, Rewa, think up at least two other ways to provide a single instance of some class.

(And, yes, all this discussion is within the funky boundaries of Java's slightly bent ability to do Singletons at all. Let's not go there again this year.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!