ACCESS PRIVATE METHOD ANOTHER CLASS USING CONSTRUCTOR IN JAVA

Maria Susan

Total Post:29

Points:107
Posted by  Maria Susan
Java 
 864  View(s)
Ratings:
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.

  1. sandy taylor

    Post:46

    Points:324
    Re: Access private method another class using constructor in java

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!