Java Java Objects Harnessing the Power of Objects Constants

Ben Kaufman
Ben Kaufman
6,813 Points

Can't seem to create a new instance of the PezDispenser Object using jshell and I can't spot why

Ok so The best thing I can do I link each section of my code

Example.java

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...

    PezDispenser dispenser = new PezDispenser("Qui Jon");

    System.out.printf("the dispenser is a %s \n",
                      dispenser.getCharacterName());

  String before = dispenser.swapHead("Darth Vader\n");
   System.out.printf("The swapped out head is %s the new head is %s\n", 
                     before,
                     dispenser.getCharacterName());
  }

}

PezDispenser.java

class PezDispenser {
  private String characterName;
  public final int MAX_PEZ = 12;
  public String getCharacterName(){
    return this.characterName;
  }


  public PezDispenser( String name) {

 this.characterName = name;

  }

  public String swapHead( String characterName){
    String originalCharacterName = this.characterName;
    this.characterName = characterName;
    return originalCharacterName; 
  }
}

Then I compiled it

javac PezDispenser.java

Then open jshell

then I entered the following

jshell> PezDispenser pd = new  PezDispenser("Yoda");                                                                                                
|  Error:                                                                                                                                           
|  cannot find symbol                                                                                                                               
|    symbol:   class PezDispenser                                                                                                                   
|  PezDispenser pd = new  PezDispenser("Yoda");                                                                                                     
|  ^----------^                                                                                                                                     
|  Error:                                                                                                                                           
|  cannot find symbol                                                                                                                               
|    symbol:   class PezDispenser                                                                                                                   
|  PezDispenser pd = new  PezDispenser("Yoda");                                                                                                     
|                         ^----------^             

I don't know why this isn't working and It is preventing me from progressing. I understand the concepts and what each piece is supposed to do, something is wonky though

2 Answers

adrian miranda
adrian miranda
12,318 Points

jshell does not know anything about the fact that you previously compiled the classes. As a result, when you attempt to create a PezDispenser, it gives you an error. Jshell hasn't heard of a PezDispenser.

If you want to be able to create a PezDispenser in jshell, you first need to tell jshell about it. The easiest way would be to use the mouse to select and copy the contents of the file, then paste them into jshell.

Tim Barbarino
Tim Barbarino
5,449 Points

Adrian is right in saying that jshell doesn't know anything about any programs that you ran previously.

I believe the issue is that you did not open the file in jshell.

You can do this with the /open command.

Ie, /open PezDispenser.java

Then, you should encounter no problems with what you were trying.