---
title: "Access private method another class using constructor in java"  
description: "Access private method another class using constructor in java"  
author: "Anonymous User"  
published: 2015-02-04  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/12939/access-private-method-another-class-using-constructor-in-java  
category: "oops"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Access private method another class using constructor in java

So, i created a [method](https://www.mindstick.com/forum/166/webservice-method) in a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) called Lotion and I [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) it read() this method takes the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)'s input. I was told by my instructor that the method read() had to be private. However, he said that his [demo](https://www.mindstick.com/forum/157370/how-to-swap-neighbor-characters-in-string-e-g-string-demo-would-be-edom)/tester class should be able to [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the method read(). He gave us a hint saying we had to use a constructor. But unfortunately, I still don't understand how it's supposed to work. Can Someone [please explain](https://www.mindstick.com/forum/344/hi-rohith-i-am-new-for-mvc-please-explain-how-to-set-implicitly-isauthenticated-true).

Thanks in advance.

## Replies

### Reply by Aryan Kumar

Sure. To access a private method in another class using a constructor in Java, you can use the following steps:

1. Import the necessary libraries.
2. Create a class with the private method you want to access.
3. Create another class with a constructor that takes an object of the first class as a parameter.
4. In the constructor of the second class, use the `getDeclaredMethod()` method to get the private method from the first class.
5. Use the `invoke()` method to invoke the private method on the object of the first class.
6. Print the result of the private method.

Here is an example of how to access a private method in another class using a constructor in Java:

Java

```plaintext
import java.lang.reflect.Method;

public class PrivateMethodAccess {

    private void privateMethod() {
        System.out.println("This is a private method");
    }

    public static void main(String[] args) {
        PrivateMethodAccess privateMethodAccess = new PrivateMethodAccess();

        // Create a class with a constructor that takes an object of the first class as a parameter
        class PrivateMethodAccessAccessor {

            public PrivateMethodAccessAccessor(PrivateMethodAccess privateMethodAccess) {
                // Use the getDeclaredMethod() method to get the private method from the first class
                Method privateMethod = privateMethodAccess.getClass().getDeclaredMethod("privateMethod");

                // Use the invoke() method to invoke the private method on the object of the first class
                privateMethod.invoke(privateMethodAccess);
            }
        }

        // Create an object of the PrivateMethodAccessAccessor class
        PrivateMethodAccessAccessor privateMethodAccessAccessor = new PrivateMethodAccessAccessor(privateMethodAccess);
    }
}
```

This code will first create an object of the `PrivateMethodAccess` class. Then, it will create an object of the `PrivateMethodAccessAccessor` class and pass the object of the `PrivateMethodAccess` class as a parameter to the constructor. In the constructor of the `PrivateMethodAccessAccessor` class, the `privateMethod()` method from the `PrivateMethodAccess` class will be invoked. Finally, the output of the `privateMethod()` method will be printed to the console.

To run the code, you can save it as a Java file and then compile and run it from the command line. For example, if you save the code as `PrivateMethodAccess.java`, you can compile and run it by typing the following commands into the command line:

Code snippet

```plaintext
javac PrivateMethodAccess.java
java PrivateMethodAccess
```

The code will then print the following message to the console:

Code snippet

```plaintext
This is a private method
```

### Reply by Anonymous User

Your current code (as posted in comments)

```
private void read() {    System.out.println(     "Enter the
amount of liquid in the bottle (0-100): ");      volume = keyboard.nextDouble();
     if (volume >
MAX || volume < MIN) { read(); } }}
```

This initializes your object by setting volume.

You can call this from the class constructor:

```
  public Lotion(){     read();  }
```

This way, when you create instances, it will automatically ask for user input and set the answer to an instance field.


---

Original Source: https://www.mindstick.com/forum/12939/access-private-method-another-class-using-constructor-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
