Why does this print null?
Hey guys. I'm making/testing a simple class that inputs a variable and a name and then I can use methods I make to print both of these values. I have made the class (Correctly, according to a solutions manual), yes when I try to print the name I get null. Can anyone tell me what i'm doing wrong?
Code:
public class Product
{
private String name;
private double value;
public Product(String named, double cost)
{
named = name;
cost = value;
}
public String getName()
{
return name;
}
public double getPrice()
{
return value;
}
public void reducePrice(int reduce)
{
value = value - reduce;
}
}
Code:
public class ProductPrinter
{
public static void main(String[] args)
{
Product jean1 = new Product("Toaster", 10);
//Product jean2 = new Product("item2", 15);
System.out.println(jean1.getName());
}
}
The print gives me null.
Re: Why does this print null?
should be
value=cost;
name = named;
what ever is on the left is the one that is getting the value a signed.
Re: Why does this print null?
Re: Why does this print null?
dear jean28,
you have defined the constructor wrongly.
It should be,
name=named;
value=cost;
becoz after the constructor is started it will get values in variable "named" and "cost"
regards,
heman
Re: Why does this print null?
Quote:
Originally Posted by
jean28
Thank you very much.
Your welcome.