Why can I change the private attribute in the class?

throwawayryan

I am using Java

public class Drink {

    private String contents;

    Drink(String theContents){
        contents = theContents;
    }



    public static void main(String[] args) {
        Drink water = new Drink("water");
        Drink oj = new Drink("orange juice");
        Drink cocaCola = new Drink("Coca Cola");

        oj.contents = "not orange juice";
        System.out.println(oj.contents);
    }
}

I thought the output should be an error. e.g.the line oj.contents = "not orange juice"; should produce an error.

If this is wrong please tell me why :) thanks

Sinan Noureddine

Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

variable content is not visible to other classes because it is private. But you can access it anywhere in the class Drink

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I change attribute of a class in Python

From Java

Why can I not instantiate a class whose constructor is private in a friend class?

From Dev

Why can I change private final ArrayList<Book>?

From Dev

Why can I use reflection to call private methods of an external class?

From Dev

Why can I use reflection to call private methods of an external class?

From Dev

Why I can not access private field of a base class when private variables can be accessed this way

From Dev

Why I can't change char array attribute of a struct?

From Dev

How can I access the value of a private attribute in an abstract class in a subclass object?

From Dev

How can I access the value of a private attribute in an abstract class in a subclass object?

From Dev

(C++ )If I declare something private in a class but it can be changed via public methods of class, then why should I declare it private?

From Dev

Why I can change/reassigned a constant value that Instantiated from a class

From Dev

Why can i change an implementation of property defined in interface in the class

From Dev

Why can't I change the speed of a CSS animation by adding a class?

From Dev

Why can't I change the custom class of my UITableView?

From Dev

Why can i change an implementation of property defined in interface in the class

From Dev

Why I can change/reassigned a constant value that Instantiated from a class

From Dev

Why can an ActionListener access private variables of the class?

From Dev

Why can a private property be accessed outside the class?

From Dev

Why can a private inner class with a private constructor be sub-classed?

From Dev

Why can a private inner class with a private constructor be sub-classed?

From Dev

Why can't I access class Meta as a attribute of Django Model Class?

From Dev

Why can I use a collection initializer with private set access from another class?

From Dev

Why can I invoke a private method on an instance of the sub-class when it shouldn't be visible to the instance?

From Dev

Why can't I access the private variable of a class after defining a friend function?

From Dev

why I can access private method in unit test class in my case

From Dev

Why can't I use a private,internal,fileprivate method within a class/static method?

From Dev

Why can I not direct-initialize a private member variable in the class definition

From Dev

Can I have private functions in a phpunit class?

From Dev

Can you declare an attribute private within an abstract class?

Related Related

  1. 1

    Why can't I change attribute of a class in Python

  2. 2

    Why can I not instantiate a class whose constructor is private in a friend class?

  3. 3

    Why can I change private final ArrayList<Book>?

  4. 4

    Why can I use reflection to call private methods of an external class?

  5. 5

    Why can I use reflection to call private methods of an external class?

  6. 6

    Why I can not access private field of a base class when private variables can be accessed this way

  7. 7

    Why I can't change char array attribute of a struct?

  8. 8

    How can I access the value of a private attribute in an abstract class in a subclass object?

  9. 9

    How can I access the value of a private attribute in an abstract class in a subclass object?

  10. 10

    (C++ )If I declare something private in a class but it can be changed via public methods of class, then why should I declare it private?

  11. 11

    Why I can change/reassigned a constant value that Instantiated from a class

  12. 12

    Why can i change an implementation of property defined in interface in the class

  13. 13

    Why can't I change the speed of a CSS animation by adding a class?

  14. 14

    Why can't I change the custom class of my UITableView?

  15. 15

    Why can i change an implementation of property defined in interface in the class

  16. 16

    Why I can change/reassigned a constant value that Instantiated from a class

  17. 17

    Why can an ActionListener access private variables of the class?

  18. 18

    Why can a private property be accessed outside the class?

  19. 19

    Why can a private inner class with a private constructor be sub-classed?

  20. 20

    Why can a private inner class with a private constructor be sub-classed?

  21. 21

    Why can't I access class Meta as a attribute of Django Model Class?

  22. 22

    Why can I use a collection initializer with private set access from another class?

  23. 23

    Why can I invoke a private method on an instance of the sub-class when it shouldn't be visible to the instance?

  24. 24

    Why can't I access the private variable of a class after defining a friend function?

  25. 25

    why I can access private method in unit test class in my case

  26. 26

    Why can't I use a private,internal,fileprivate method within a class/static method?

  27. 27

    Why can I not direct-initialize a private member variable in the class definition

  28. 28

    Can I have private functions in a phpunit class?

  29. 29

    Can you declare an attribute private within an abstract class?

HotTag

Archive