-3

Is it possible to have private objects? For example, when you instantiate an object (classType object1 = new classType()), can that be private or protected or is it always public?

5
  • 1
    It can be private or protected. Apr 1, 2016 at 4:47
  • 2
    AFAIK, fields can be private and can be objects. Apr 1, 2016 at 4:52
  • 1
    Relevant post: Which is the default access specifier in Java? Apr 1, 2016 at 5:45
  • Close votes for "unclear" are not appropriate here. The problem is that the asker has a lack of understanding, and therefore an appropriate answer is to point out what he has misunderstood.
    – Jules
    Apr 1, 2016 at 10:27
  • 2
    @Jules: I didn't VTC, but I can understand those who did. For example, it is completely unclear what the relationship to c++ is. (Does C++ have private objects?) Also, the OP didn't specify what exactly he means by "private object". Is he comparing to Scala, maybe, which definitely does have private objects? In my answer, I chose to simply ignore those questions, but I think it is just as reasonable to VTC based on those. Apr 1, 2016 at 10:40

3 Answers 3

5

The question of whether an object is private or protected or public simply doesn't make sense: you cannot name an object, and thus you cannot access an object by name, so access restrictions based on names simply do not apply to objects.

The only way to access an object is if that object is either referenced by a field or returned by a method, both of which can be public, protected, package protected, or private.

0

Is it possible to have private objects?

Not in the strict sense of private. This word only makes sense when talking about members/methods of a class and has no other place.

In a broader sense, it is possible (and quite common) to create objects inside a class in a way that they never leave the class, and thus are invisible to the outside world (i.e., store them only in private members of the class, and make sure you never use them in a way that exposes them to the caller of your methods). Still, the object is not private; only the member of your class that contains a reference to that object is in a strict sense private.

-1

Java objects instantiated with new operator (ie. Example eObject = new Example()) can't be specified private or public. They are limited by the scope and visibility based on context they are defined in. For example, an object created inside a method can be accessed from within that method only.

But objects composed within other objects (ie. instance variables) can be specified with private, public, protected access specifiers.

3
  • 1
    All Java objects are instantiated by the new operator. Objects are not directly composed in Java, they are only aggregated by reference. The fields that hold those references have an access specifier, but the object itself does not.
    – Jules
    Apr 1, 2016 at 10:31
  • @Jules: You're right. The references have the access specifier not the actual objects.
    – sshah
    Apr 1, 2016 at 11:51
  • @Jules All Java objects are instantiated by the new operator. Except the ones that are instantiated via deserialization. Not that it changes the validity of your comment, but it's an often overlooked fact of life in Java.
    – biziclop
    Apr 1, 2016 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.