0

Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?

public A()
{
    private A()
    {
         //some code....
    }

    protected A()
    {
         //some code....
    }
}
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Sandeep
  • 17
  • 1
  • 3
  • 1
    http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Brian Roach Jan 06 '13 at 03:23
  • If you understand what the access modifiers mean there is no question here, and if you don't you should look them up in the JLS. Not a real question. – user207421 Jan 06 '13 at 21:52

1 Answers1

6

It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.

public - any one can call the constructor from anywhere in the code.

private - Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here

protected - Like private but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.

As @dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87