Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods

 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain this code step by step:
specifically the methods

class AnIntegerNameX
int x;
public int x(){
return x;
}
public void setx(int newX){
x= newX;
}
}
 
kavya krushi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Becky,
setx() method of your code is used to set an integer value while the x() is used to return that valus ( set by setx() method) for out side use
I'd added a main to your code for better understanding ...
-----------------------------------------------------------------
class i
{
static int x;
public static int x()
{
return x;
}
public static void setx(int newX)
{
x= newX;
}
public static void main (String arg[])
{
i b = new i();
b.setx(10);
int num = b.x();
System.out.println("Integer : "+num);
}
}
-----------------------------------------------------------------
If you see the main method , b is an object and the setx() method is used to set an integer value 10 ... And the x() method is called to use this value ... now num holds the integer value and can be used anywhere ...
kavya
 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kavya,
so this method sets a variable x to newX, then passes that value with the code (int newX)?
public void setx(int newX){
x= newX;
}
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code with an added main method to demmonstrate.
You have an instance variable x which you would like to modify it. Now it's bet to make it private because you are providing a getter and a setter which modify and retrieve the state of the x. So if it's not restricted then there is no need for the set and get methods in this class. Now do you understand how method calling works ? Let's look at the method setX which takes an int as a parameter. let's say we call your method like this x.setx(10) where x is the reference to your object class. what happends here x is calling a method of your object setx and passing a copy of a value 10 to it. so the variable newX now holds the value of 10 and places it into an x where you have an assignment. Your method x returns the value of an x. I provided some comments there. If you need further help ask.
class AnIntegerNameX {
int x;
public int x(){
return x; // this returns a current value that is stored in x
}
public void setx(int newX){
x= newX; // assignemtn of newX to an x is made here
}
public static void main(String [] args) {
AnIntegerNameX x = new AnIntegerNameX();
x.setx(10); // call methd setx and passing a copy of a 10 to it
}
}
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becky,
so this method sets a variable x to newX, then passes that value with the code (int newX)?
public void setx(int newX){
x= newX;
}

You have the first part right, about the method setting x to newX. But the (int newX) code in the method declaration is to allow the method to accept a parameter (newX) when it is called. That is the only purpose (int newX) serves...to get a value passed into the method. So if we were to say:

the result is objectX.x = 10 (the variable x in the object objectX becomes 10). Make sense?
Anything in parentheses after a method declaration is there to pass values (parameters) into the method. It has nothing to do with getting anythnig out of the method...that is done with the keyword return in the method, along with a return type in the method declaration.
~Ryan
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becky,
After looking at your question again, I think you may have the right idea...but your use of the word "then" had me thinking that you thought (int newX) was used to pass a value out of the method...
At any rate, my above reply should (hopefully) make it all the clearer
~Ryan
 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,
okay, I think I finally got it now. ObjectX.setx(10) calls the method setx and passes the value 10 into the method and sets the x variable in object ObjectX to 10. This object and method concepts are hard to grasp.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic