JAVA - Beginner - Can a private class attribute be accessed outside of the class?

StackOverflow https://stackoverflow.com/questions/12196185

  •  29-06-2021
  •  | 
  •  

سؤال

I am relatively new to Java OO Programming, and have reviewed the questions similar to this, although they don't seem to answer my question directly.

Basically, I understand that if a data member within a class is declared as private, then it is only accessible from within that same class.

My lecturer always advises that ALL attributes should be declared as private - why is this?

Now I am familiar with using GET methods, and my question is, can a private attribute be accessed outside of it's own class via invoking a PUBLIC 'get' method (which returns the aforementioned attribute) from another class?

For example:

public class Class()
{

    private int number = 0;

    public Class()
    {
    }

    public int getNumber()
    {
        return number;
    }

}

And then from another class:

public class Class2()
{

    Class class = new Class();

    public void showNumber()
    {
        System.out.print(class.getNumber());
    }
}

Will the second block of code allow the method in showInt() inside Class2 to actually access the private attribute from Class?

I guess I am really struggling with deciding whether any attribute or method should be declared public or private in general..

Is there any particular rule of thumb that should be followed?

Thank you responders for any assistance.

Kind regards

هل كانت مفيدة؟

المحلول

The key point is that a public get method only returns the current value of your private member, without giving access to the member itself. This means that you cannot somehow "get" the member and change its value.

نصائح أخرى

My lecturer always advises that ALL attributes should be declared as private.

Good advise. There are exceptions to this rule but I would start with this.

I understand that if a data member within a class is declared as private, then it is only accessible from within that same class.

Its accessible to

  • nested classes in the same file.
  • access via reflection.

Will the second block of code allow the method in showInt() inside Class2 to actually access the private attribute from Class?

Effectively yes in this case. With the use of a getter, the field can change name, be replaced by a constant or a calculation or be logged and Class2 doesn't need to know.

Yes, if you make a public accessor, you may access the private field.

The point is to hide the implementation so that, as an implementor, you may decide later to have another type of field (or an indirection, or a dynamic computation) without forcing the users of your class to change their code.

It also lets you offer a getter while not offering any setter (then your field is read only from outside the class).

Many things wouldn't compile in your code :

  • add parenthesis to call a method (getInt)
  • class is a reserved word
  • a class can't be called Class and isn't declared as you did

I'd suggest you write a compiling code in an editor (e.g. Eclipse) before asking a question. This task would probably help you understand at least part of the problems.

Yes.

You will have to call the method though (and you cannot use class as a class name):

public class Class2()
{

    Class myClass = new Class();

    public void showInt()
    {
        System.out.print(myClass.getInt());
    }
}

What happens is that you do not get access to the private member, but you receive a copy of the value. In this case, you access the method getInt(), and not the attribute. If you would try to access the attribute:

public class Class2()
{

    Class myClass = new Class();

    public void showInt()
    {
        System.out.print(myClass.x);
    }
}

you will not be given access.

Short answer: no.

The general reason for making everything private is to make sure that users of your class can't modify things in ways they aren't supposed to.

What you can do is define getter and setter methods. Generally it's best to only expose getters, e.g.

private int foo;

public int getFoo(){
  return foo;
}

This way, people can see foo but not change it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top