Harry Joy
Harry Joy

Reputation: 59650

Question about constructors in Java

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.

Upvotes: 3

Views: 3195

Answers (8)

Ananthi
Ananthi

Reputation: 11

  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

Upvotes: 0

sandy
sandy

Reputation: 1

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.

Upvotes: 0

Chris Thompson
Chris Thompson

Reputation: 35598

  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)

Upvotes: 2

Paul Sasik
Paul Sasik

Reputation: 81429

  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.

Upvotes: 1

Thilo
Thilo

Reputation: 262464

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.

Upvotes: 3

Charlie Martin
Charlie Martin

Reputation: 112346

  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.

Upvotes: -1

kvista
kvista

Reputation: 5059

  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).

Upvotes: 0

Amir Afghani
Amir Afghani

Reputation: 38511

  • 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.

Upvotes: 0

Related Questions