• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with some code?

 
matt gilbert
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class CookPot
{
Private int capacity, material, price;
Public void getPot (int size, int metal ,int cost)
{
size=capacity;
metal=material;
cost=price;
}
}
This code is just not working they way I want it to. I think it has something to do with the variables capacity, material, price and the way they are defined.
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this isn't the real code because some of the keywords are capitalized--but do you really have the parameters to getPot on the left side of the assignment operator and the member variables on the right? If so, just swap them. If that isn't the case, maybe posting the real code and a more detailed description of the problem might be in order.
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi matt,
[B]
[/B]
looking at the codes, i can tell you two things.
First, since your method name starts with get, you must be trying to get values of each of your privage variables. if this is the case, you getter method must have a return type based on variables and you must not declare a parameter list inside this getter.
Second, as you defined a parameter list, you are intending to set values to those private variables. you don't set values through getter method, but through set method. so you need to redesign your class like following:

hope this might help. you might want to take a look at some OO literature that is in sun site.
 
matt gilbert
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do the variables size, metal and cost exist only in the scope of the getPot method?
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameters to the getPot method are 'in-scope' only in that method. The values passed to a call of that method may have a much longer lifetime. If you are trying to extract the values of your private data members into the parameters to getPot, you can't do that in Java because parameters are copies--while you can modify their value within the method, that change does not affect the actual value passed into the method call.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic