Question about constructors in Java

前端 未结 8 639
我寻月下人不归
我寻月下人不归 2021-01-20 14:52

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?
相关标签:
8条回答
  • 2021-01-20 15:27
    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.

    0 讨论(0)
  • 2021-01-20 15:28

    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.

    0 讨论(0)
  • 2021-01-20 15:29
    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).

    0 讨论(0)
  • 2021-01-20 15:29
    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.
    0 讨论(0)
  • 2021-01-20 15:30
    • 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.
    0 讨论(0)
  • 2021-01-20 15:30
    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
    0 讨论(0)
提交回复
热议问题