Home > DeveloperSection > Forums > Access private method another class using constructor in java
Maria Susan

Total Post:29

Posted on    February-04-2015 10:16 PM

 OOPS Java 

 1 Reply(s)
 523  View(s)
Rate this:

So, i created a method in a class called Lotion and I named it read() this method takes the user's input. I was told by my instructor that the method read() had to be private. However, he said that his demo/tester class should be able to access 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.

Thanks in advance.

sandy taylor
sandy taylor

Total Post:46

Posted on    February-04-2015 10:39 PM

Your current code (as posted in comments)

private void read() {


     "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(){



This way, when you create instances, it will automatically ask for user input and set the answer to an instance field.

Don't want to miss updates? Please click the below button!

Follow MindStick