0

I am new to java... If I am missing something, please advise me.

I try to access the private method like below:

public class PublicClassPrivateMethodAndVariableAccess
{
    private int a=23;

    private void show()
    {
        System.out.println("a = "+a);
    }

    public static void main(String... arg)
    {
        PublicClassPrivateMethodAndVariableAccess pr = new PublicClassPrivateMethodAndVariableAccess();
        System.out.println("using method showing  pr.show() ");
        pr.show();
        System.out.println("pr.a = "+pr.a);
    }
}

This will show output as:

using method showing  pr.show()

a = 23

pr.a = 23

But when I’m using a similar code with a different way to access the private method...:

class Testclass
{
    private int a=23;

    private void show()
    {
        System.out.println("a = "+a);
    }   
}

public class NoModifierClassPrivateMethodAndVariableAccess
{
    public static void main(String... arg)
    {
        Testclass pr = new Testclass();
        System.out.println("using method showing  pr.show() ");
        pr.show();
        System.out.println("pr.a = "+pr.a);
    }
}

... it shows error as below:

NoModifierClassPrivateMethodAndVariableAccess.java:19: error: show() has private
 access in Testclass
                pr.show();
                  ^
NoModifierClassPrivateMethodAndVariableAccess.java:20: error: a has private access in Testclass
                System.out.println("pr.a = "+pr.a);
                                               ^
2 errors

I am asking why this second code is failing to access the private method?

abu
  • 19
  • 4
  • 1
    A `private` method is only visible in the class that contains it. Example 1: Everything is in `PublicClassPrivateMethodAndVariableAccess` and thus `show()` is visible. In Example 2: You put the method in another class, so the method isn't visible. Make it `public`. – Elliott Frisch May 26 '18 at 15:32
  • https://stackoverflow.com/questions/215497/in-java-difference-between-package-private-public-protected-and-private?rq=1 – Michael May 26 '18 at 15:32
  • _"Make it `public`"_ Or keep it private and add a getter and/or setter. – Michael May 26 '18 at 15:33
  • thanks i understand..one more in my code, is Testclass is public or private class? – abu May 26 '18 at 16:35

3 Answers3

0

You need to make a getter for your private item.

You cannot directly access a private item which is exactly what you are trying to do when you enter

System.out.println("pr.a = "+pr.a);

instead you should use a getter such as

   public int getA(){
       return a;
}

so your new display should be something like

System.out.println("pr.a = "+pr.getA());
nickzor
  • 84
  • 7
0

A private field a can only be accessed by its owning class A_Class itself. For example:

public class A_Class {
    private int a = 3;
    private void show() {
        System.out.println(a); // output is 3
    }
}

Here we are:

  • inside A_Class
  • accessing private field a

That is what you are doing in your first code and that works fine.


But, in your second code you are in class B_Class trying to access the private field a that is owned by class A_Class and that is not possible. For example:

public class A_Class {
    private a = 3;
}

public class B_Class {
    void show() {
        private A_Class aclass = new A_Class();
        System.out.println(aclass.a); // error! a has private access
    }
}

Here we are:

  • inside B_Class
  • creating an object of class A_Class
  • trying to access the field a, that has private access only to class A_Class - this fails!

Here or here you can find some more explanation to java modifiers.

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
  • i ask u one more thing ...in my code, is Testclass is public or private? – abu May 26 '18 at 16:16
  • `Testclass` is neither *private* nor *public*. `Testclass` has no modifier and thus it is *package*-visible. That means, only the classes that are in a package together with `Testclass` can access (see) `Testclass`. Maybe this will also help you: https://www.decodejava.com/java-class-access-modifiers.htm – Rene Knop May 26 '18 at 20:39
0

From Oracle docs:

+-------------+-------+---------+----------+-------+
|  Modifier   | Class | Package | Subclass | World |
+-------------+-------+---------+----------+-------+
| public      | Y     | Y       | Y        | Y     |
| protected   | Y     | Y       | Y        | N     |
| no modifier | Y     | Y       | N        | N     |
| private     | Y     | N       | N        | N     |
+-------------+-------+---------+----------+-------+

In the first case you where calling the show method from same class which is possible as per doc.

In second case your calling a private method from different class which will cause compiler error.

If you really want to access a private method from different class , then you want to look into Reflections in Java.

Example:

import java.lang.reflect.Method;

class Testclass
{
    private int a=23;

    private void show()
    {
        System.out.println("a = "+a);
    }   
}

public class NoModifierClassPrivateMethodAndVariableAccess
{
    public static void main(String... arg) throws Exception
    {
        Class<?> clazz = Testclass.class;
        Method method = clazz.getDeclaredMethod("show");
        method.setAccessible(true);
        System.out.println(method.invoke(Testclass.class.newInstance()));

    }
}
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53