0

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.

jiija
  • 21
  • 2
  • 7
  • Are you sure he said constructor or reflection? – shikjohari Feb 05 '15 at 05:37
  • Possible duplicate for http://stackoverflow.com/questions/11282265/how-to-call-a-private-method-from-outside-a-java-class – shikjohari Feb 05 '15 at 05:38
  • He said constructor. Is it even possible.Also, what's reflection? – jiija Feb 05 '15 at 05:39
  • 3
    He probably meant that you should have another (public) method or constructor that calls the private method internally, thereby making it indirectly accessible. I'm not sure taking user input from a constructor is good practice, though. – Thilo Feb 05 '15 at 05:39
  • ^ As Thilo says, probably via the means of the `super` constructor... – MadProgrammer Feb 05 '15 at 05:40
  • @Thilo that is what he was talking about. But how do I do that. Can you please give me an example? – jiija Feb 05 '15 at 05:40
  • Can you post some code first? Hard to say anything without context. But keep it to the relevant parts. – Thilo Feb 05 '15 at 05:41
  • @MadProgrammer can you please elaborate on super constructor. I'm new to programming. – jiija Feb 05 '15 at 05:42
  • 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(); } } – jiija Feb 05 '15 at 05:43
  • In the constructor of Lotion class call the read method. This is how you can easily access the private method. – Vivek Singh Feb 05 '15 at 05:43
  • Sorry about the code in the comment section. How do I post it? – jiija Feb 05 '15 at 05:44
  • One of the implementation is shown by @shikjohari... Please have a look... – Vivek Singh Feb 05 '15 at 05:47

5 Answers5

3

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.


Some design notes (but don't get into a fight over it with your instructor):

  • "heavy lifting" like user input should probably not be done as part of the constructor.
  • if you are going to call methods from the constructor, making them private is a good idea. Otherwise they can be overridden, and then will be called on a not completely constructed object instance.
  • read could return a double instead of updating the object state directly.
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thank you, I didn't know that it would automatically ask for it. I thought I had to call it like other methods. For example: name.read(); – jiija Feb 05 '15 at 05:53
  • Well, if you put it into the constructor, it will do all that automatically. Which is maybe we one should not put it there (to have more control). But that's a design question. – Thilo Feb 05 '15 at 05:54
  • But the code is working now, so thank you. Your a project saver :-) – jiija Feb 05 '15 at 05:54
2

Make use of reflection and use setAccessible method to access the private constructor.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • 5
    Very unlikely that this would come up in a beginners' Java course. – Thilo Feb 05 '15 at 05:42
  • Agreed, I doubt the point of this exercise is to use Reflection. The point is most likely to think about the different scopes available in the language. You don't need to use Reflection to do what is being asked here. – Kevin Hooke Feb 05 '15 at 05:46
1

Below is the solution.:) next time I would prefer you to do some coding before asking...:P

public class Lotion{
        public Lotion(){
            System.out.println(read("Constructor"));
        }

        private String read(String input){
            return input;
        }
    }
    class Tester{
        public static void main(String[] args) {
            new Lotion();
        }
    } 
shikjohari
  • 2,278
  • 11
  • 23
0
  1. As your read() method is private we can't use it directly.
  2. just create object of lotion class in demo class.As we are creating object we are calling constructor of lotion class.
  3. pass user input as argument of that constructor
  4. inside constructor call read() method.
0

I think your instructor is hinting where private methods can be called from and how you should structure your code. Private methods can only be called (by default, without resorting to under-the-covers techniques like bytecode manipulation, or even Reflection, neither of which are probably the point of this exercise) from within the same class. This would also include the constructor. If you think about this some more, you should have everything you need...

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33