21

I have two classes in two different packages. For one class I've defined a constructor without setting access modifier for it. I want to instantiate an object of this class in another package and get the error 'the constructor xxx() is not visible'.

If I define access modified to public it is fine. I thought constructors are public by default?

JuanZe
  • 8,007
  • 44
  • 58
Eugene
  • 59,186
  • 91
  • 226
  • 333
  • Class constructors are package-private by default. Enum constructors are private by default. – Boann Oct 18 '16 at 00:48
  • The only constructor that's public by default is the implicit, no-arguments one. That is if you don't define any. – h4nek May 30 '20 at 14:54

7 Answers7

29

no access specifier != public

No Modifier is package private. check doc

jmj
  • 237,923
  • 42
  • 401
  • 438
13

No, they're not. They have package-visibility by default.

skaffman
  • 398,947
  • 96
  • 818
  • 769
8

Access is (err...) default access by default. Also known as package private. Consider: if they were public by default, how would you indicate that a constructor was not public but in fact was package private? There is no keyword corresponding to package private with which to indicate that.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
4

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor. If you don't declare the constructor explicitly as public it is visible only in the same package where it was declared (package access).

JuanZe
  • 8,007
  • 44
  • 58
2

When you don't write access modifier it is set to default, which means package private. E.g. no class outside the package can access it.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

No. they have the default access specifier. i.e they have package visibility.

rgksugan
  • 3,521
  • 12
  • 45
  • 53
1

In a class all method without access modifier have package visibility. However, in interfaces methods always have public visibility.

bertolami
  • 2,896
  • 2
  • 24
  • 41