0
public class Store {

  public Store(String product, int count, double price) {
  
  }

  public static void main(String[] args) {
    Store cookieShop = new Store("cookies", 12, 3.75);
    System.out.println(cookieShop);
  }
}

I can't seem to print out the values I assigned to cookieShop.

Expected output:

cookies123.75

Output:

Store@2aae9190

Beginner here. Thanks.

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • 1
    you haven't assigned anything, you just passed some values as parameters, but you're not using them. You also haven't overwritten the toString method. – Stultuske Sep 11 '20 at 06:02
  • You're passing values into constructor and then?.. what happens in the constructor? you just discard those values as you don't store them anywhere. Therefore, your next statement `System.out.println(cookieShop);` will print nothing, as there is nothing to print. – Giorgi Tsiklauri Sep 11 '20 at 06:03
  • @GiorgiTsiklauri it does print something: the result of the toString method. It's even put in the question – Stultuske Sep 11 '20 at 06:04
  • You need instance variables in your Store class to hold the values passed through the Constructor. – DevilsHnd - 退職した Sep 11 '20 at 06:05
  • @Stultuske, well, it prints the object reference toString, which is not the data, per se. What I mean is a payload which OP tries to store. – Giorgi Tsiklauri Sep 11 '20 at 06:16

3 Answers3

1
  1. You are doing nothing in your constructor.
  2. If you print this object without overriding the toString(), you will get a hexadecimal representation. Do something like below
public class Store {

  String product;
  int count;
  double price;
  public Store(String product, int count, double price) {
  
      this.product=product;
      this.count=count;
      this.price=price;
  }

  public static void main(String[] args) {
    Store cookieShop = new Store("cookies", 12, 3.75);
    System.out.println(cookieShop);
  }

  @Override
   public String toString() {
    return "Store [product=" + product + ", count=" + count + ", price=" + price + "]";
  }
  
  
}
Nish
  • 922
  • 13
  • 31
0

Hi it's beacause you never assing the input varabiables in the constructor. Try to define some class variables (private String product;private int count;private double price) and in the constructor assign the input to the class varabiables. Than create getter and setter method for the class variables and override toString() method (this operations can be easily delegated to youd IDE i.e Eclipse or Intellij). Rerun the program and see what happens.

Giovanni
  • 3,951
  • 2
  • 24
  • 30
  • why the getter and setter? – Stultuske Sep 11 '20 at 06:07
  • It's for learning purpose, I think that adding getter and setter helps the poster to understand some Java OOP basic. writing a useful class with all instances variables without getter and setter is a very rare sistuation (if ever) – Giovanni Sep 11 '20 at 06:13
  • yes, but the question isn't about that, it's about the print on it's own. There is a difference by commenting that it's good practice to add them, but you make it seem they are required for what the OP is trying to do, which is not the case. – Stultuske Sep 11 '20 at 06:47
-2

You need to define a toString method for your Store class. By default, all objects are using the toString method from java.lang.Object (https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) class if it's not overridden in your class, which simply prints the name of the class and the hexadecimal representation:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Try something like this (using your own field names):

@Override
public String toString() {
    return name + count + price;
}

Consider also using libraries like lombok, which makes all of that stuff really easy, simply adding the corresponding annotation.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jorge
  • 1,136
  • 9
  • 15