3

I have few questions regarding Java constructors

  1. Can a constructor be private? If yes then in which condition?
  2. Is a constructor a method or not?
  3. If a constructor does not return anything then why we are getting a new Object every time we call it?
  4. What's the default access modifier of a constructor if we do not specify.

Edit

The answers for 1 & 3 are very clear. I'm still not sure about 2 & 4 since I'm getting different answers for them.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • yes. I would suggest asking the part about Tomcat on http://www.serverfault.com – Chris Thompson Jan 13 '11 at 04:25
  • @Chris: Okay will do it. Thnx – Harry Joy Jan 13 '11 at 04:27
  • 1
    For #4 the consensus seems to be package private. The answers that specify public are just partially correct in that the public visibility is limited to the package. #2 looks like consensus too: It is a method. The explanations just use different terminologies. – Paul Sasik Jan 13 '11 at 04:43
  • 1
    for #4, the default is not package private (i.e. default). All classes extend java.lang.Object, which contains a public constructor, and through inheritance, the subclass inherits this public constructor. The answer is, therefore, public. When in doubt, test it out. – hisdrewness Jan 13 '11 at 06:11

8 Answers8

3

Can a constructor be private? If yes then in which condition?

Yes. There are no conditions. Of course, no one except the class itself can call it then.

This is actually a frequent pattern: Have a static getInstance() and keep the constructor private.

There can also be private constructors that the public constructors internally call.

Constructor is a method or not?

Hmm. I say "no". At the very least, it is a "very special kind of" method. In what context exactly? The terminology is less important than what you are trying to do.

If constructor does not return anything then why we are getting a new Object every time we call it.

The new operator returns something (the new instance).

Whats the default access modifier of a constructor.

Same as for methods. Package-private.

If you do not specify any constructor, the class gets a default constructor, which takes no arguments, does nothing except calling the parent constructor and is public.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    the default constructor only will be public if the class is public. From JLS: "In an enum type, the default constructor is implicitly private. Otherwise, if the class is declared public, then the default constructor is implicitly given the access modifier public; if the class is declared protected, then the default constructor is implicitly given the access modifier protected; if the class is declared private, then the default constructor is implicitly given the access modifier private; otherwise, the default constructor has the default access implied by no access modifier. " – user85421 Apr 12 '11 at 22:14
2
  1. Yes, in any case. However, if all constructors for a class are private, that means that the class cannot be directly instantiated. You will need to use something like the Factory Pattern to create instances of the object.
  2. Yes, the constructor is a method.
  3. A better way to think about it is that the new operator returns the object and in the process of creating the object, calls the constructor. Another way to think about it (although this is only a way to think about it, it isn't technically correct) is simply that the return type is implied by convention. A good place to read more about this is to read about new in the context of C++. The constructor's role is not to create the object but rather to initialize the memory contained within the object.
  4. Default access for a constructor in Java is package private just like any other method. (One such source: http://www.javabeginner.com/learn-java/introduction-to-java-access-modifiers and from the horse's mouth: http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html)
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • if constructor is method then why don't it return anything? Because as per java method it must return something or use can also void. but something must be there in return type of a method??? – Harry Joy Jan 13 '11 at 04:44
  • Ah yes, you're right. As you explore this topic you'll also discover that, unlike other methods, a constructor cannot be inherited. It is a method, it's just a special type of method. – Chris Thompson Jan 13 '11 at 04:47
  • 1
    Default access is default, which is different from private or protected. Default methods can be accessed by classes in the same package. Protected methods can be accessed by implementations (classes that extend the method) or within the same package. Private can only be called within the scope of the class by itself, or inner classes. The default access of an object is public, because all classes extend Object, which contains the public Object() {} constructor. The builder pattern is another creational pattern that makes use of private constructors. – hisdrewness Jan 13 '11 at 06:06
  • @hisdrewness You are absolutely right, that was a complete bonehead on my part. I've updated my answer to eliminate that inaccuracy. – Chris Thompson Jan 13 '11 at 16:29
1
  1. Yes, constructors can be private. This is done when you want tighter or alternate control over instance creation such as with factory methods or with a pattern such as a Singleton.
  2. It is a method but it is not called directly. It is a special type of method invoked on your behalf when you create a new object.
  3. Constructors don't return anything, they create new objects.
  4. The default is package private. So public to any class within the package but not visible to code outside of the package.

Thoughts on Tomcat performance and scalability: This is a highly variable situation based on your server hardware and types of requests and of course the quality, efficiency and memory footprint of the code serving each request.

Your lower bound on concurrent requests was 500. Consider that you probably want to create a thread for each request and given a 1MB stack per thread you're looking .5 GB just for thread stack space. And this is before heap memory and the performance overhead of allocating that many threads. I think that if need to handle that many requests at a time you might want to consider a more heavy duty server like JBoss.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
0
  1. Constructor can be created as a private in any case.
  2. Constructor is a special type of method which can be automatically called when we are creating object for the corresponding class.
  3. Constructor does not contain any return values. It just create new objects. Should not provide any return type for constructor.
  4. The default access specifier of the constructor is public
Ananthi
  • 11
  • 1
0
  • A constructor can be declared private for any class.
  • A constructor is a special method that returns an instance of the class it belongs to, therefore you do not need to specify a constructors return type.
  • Package private is the right answer as pointed out below.
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • "There is no default access modifier for a method" If you do not specify anything, it is package-private. – Thilo Jan 13 '11 at 04:23
  • Actually a constructor does not return anything, it is the `new` operator that creates the instance and calls the constructor of/on that instance (so we can use `this` in the ctor). – user85421 Apr 12 '11 at 22:28
0
  1. Yes -- factory instance singletons often use this pattern, to force users to init their class via static factory method.
  2. Yes, it's a method
  3. Because that is what a constructor does - it constructs. (it's assumed the result of construction will be returned)
  4. same as methods

With regards to your Tomcat question, it depends on which version of Tomcat, which IO model it's using (e.g., NIO versus historical network IO modules), and your configuration. Single Tomcat's can process hundreds of requests at a time, although the concurrency is tune-able (each request will be handled by a distinct thread or thread from a pool).

kvista
  • 5,039
  • 1
  • 23
  • 25
0

The default access modifier of a constructor is CLASS ACCESS MODIFIER, If a class is public , then access modifier of a constructor is public. If the class is default , then constructor is also default.

heldt
  • 4,166
  • 7
  • 39
  • 67
sandy
  • 1
-1
  1. Yes.
  2. Yes.
  3. because a constructor is called by new. What returns the object is the new, the constructor simply sets up the internal state.
  4. Public.
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • "4. Public"? Ambiguous question I guess. The implied default constructor is public, but a constructor without modifiers is package-private. – Thilo Jan 13 '11 at 04:27
  • @Thilo: I'm asking about constructor without modifier. – Harry Joy Jan 13 '11 at 04:30