• Post Reply Bookmark Topic Watch Topic
  • New Topic

Back to basics - understanding methods.  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get back to basics and understand Methods. I am working on a very simple code. A variable of num is set to 1 in the Main.

-A person is asked to input their name (as a method) and outputs the name and number (according to the variable set in the main),
-then a second method where it repeats the name and the number and outputs the results.

Since it does not work, am I right to understand that Method will not return the values that are created or discovered in those methods? Will methods even take information from the Main? I know the VOID tells it that you do not RETURN anything, so what would I use instead of VOID?

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you even get that code to compile?
Start by going through the Java® Tutorials and finding out about methods, also about scopes of variables.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it trying to understand this - it does not work because it says that "num" is not a variable it recognizes. Therefore, it shows that even though I made the variable "num" in the Main, it did not carry it forward to the method.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I thought. The variable num is written inside the main method, so it only exists inside the main method. By the time you reach the other method, num has gone out of scope and ceases to exist.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you tie yourself in even more knots, consider creating an object which encapsulates the number guessed and the name of the person.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is what I thought. The variable num is written inside the main method, so it only exists inside the main method. By the time you reach the other method, num has gone out of scope and ceases to exist.
I would phrase this a bit differently. The variable num is not accessible from within the method because it is local to main(). It still exists. When you return from the method back to main() you will once again have access to num. You can resolve this by making num a class level variable (aka field) or by passing num as a parameter to your method.

Edit: I take that back. You cannot use a class level variable in your case because you don't have an instance of your class 'test5'.
FYI: Class names should begin with an upper case letter.

http://www.java-made-easy.com/variable-scope.html
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variables declared inside a method are only available inside that method. If you step out of it temporarily by calling another method, it still exists, but that other method does not know about it.

What can you do?

well, you could pass it in from main() to your enterGuess() method, but it only sends in a copy of the value. Just like if you give me a photocopy of a piece of paper you have, if I change MY copy, YOURS remains unchanged. Sometimes that is what you want, and sometimes it isn't. What you do depends on your specific requirements.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:I am trying to get back to basics and understand Methods. I am working on a very simple code. A variable of num is set to 1 in the Main.

-A person is asked to input their name (as a method) and outputs the name and number (according to the variable set in the main),
-then a second method where it repeats the name and the number and outputs the results.

Since it does not work, am I right to understand that Method will not return the values that are created or discovered in those methods? Will methods even take information from the Main? I know the VOID tells it that you do not RETURN anything, so what would I use instead of VOID?







I could be wrong here but why are you using the return statement when your using a "void" method.normally this only done for methods with a return type i.e int,double,etc but then again I could be wrong.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:I could be wrong here but why are you using the return statement when your using a "void" method.normally this only done for methods with a return type i.e int,double,etc but then again I could be wrong.

It is not a compile time error. In fact, about this particular situation you're right, there's no logical reason to use return statement - it is just redundant.
But could be a situation when it would have an effect.

For instance:
So, if amount would be passed negative, the method just would stop executing after the 3rd line (method would exit). It is just an example, it is not the right way to approach to such a problems.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I started to read some java material and came up with this on my own (after seeing it and studying someone else's code of course). I think I am getting the idea of methods, but not sure how to return value or even to jump back to the beginning of a method.
I am sure there are more elegant and shorter ways of making this work, but at least I understand it. The challenge is the PLAY AGAIN method, where I want the program to run, and then the player will ask to play again, if they say yes, I want it to RETURN to another method, if they say NO, it just exits. As it stands, I see that it is RETURNING to the PLAY AGAIN method, but since there is nothing after that, it just finishes. Can I do a RETURN (to another method)?

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can I do a RETURN (to another method)?
In a word, no.
You can only ever return to the method that called your current one.
One method can call another method which calls yet another method - but you can always trace back down the 'stack' of methods to see how you got to this point, and you know that it will always return control to those calling methods.
Having it structured like this makes it much easier to keep track of what is happening.

The point of a return is be able to pass information back to a calling method
for instance a trivial add function:


This method declares the result of calling it will be an int. (that is the reason for the 'int' in 'public int add' method declaration.
At some point the method needs to 'return' that value.
A "void" method says it doesn't return a value.
Methods are limited to return one value. But as you learn, you will find how to manage that :-)

now if I wanted to call this method and use its answer:




So I would suggest you change your playAgain method just ask the user and return what the user said.



and then in your calling code:



Also, we probably need to discuss how to compare Strings, and the difference between == and .equals.



You can read all about that in AvoidTheEqualityOperator
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are great help!
the boolean isn't a method, so how does it fit into the Main? (line 9 through 14). in line 10, while KEEPPLAYING is true, it calls the METHOD, PlayGame, right?

Once PLAYGAME method is finished (line 43), it must jump into the PlayAgain (boolean) method (line 45), right? I am confused here. Does this method ever get called or does it automatically enter it?

The return true (line 53) must mean that the Boolean is true and it goes back to the boolean Keepplaying (line 9), right?

I am not sure how booleans work though.



 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, we ARE right at the basics aren't we?

>the boolean isn't a method, so how does it fit into the Main? (line 9 through 14).
boolean is one of the primary java data types.
It represents true/false values. A boolean can either have the value 'true' or the value 'false'
This line declares a boolean variable called 'keepPlaying' and sets its value to true. Later that value might change.

>Once PLAYGAME method is finished (line 43), it must jump into the PlayAgain (boolean) method (line 45), right? I am confused here. Does this method ever get called or does it automatically enter it?
Almost.
First it has to go back to main and look at the next line of code - which just happens to include the playAgain() method.

Every computer program has a flow of control. It starts at main and executes the code line by line until it reaches the end.
Calling a method sends the control into that method.
When that method finishes, you go back to where you called it, and keep going from there.


Tracing through from the beginning, we call the main() method.

line 9 - declare a boolean variable 'keepPlaying' - give it the value true
line 10 - set up a loop as long as the value of 'keepPlaying' is true
line 11 - call playGame() method

So we go and execute the code in the playGame() method (lines 19-43).
After the playGame method control keeps going from this point

line 12 - call the playAgain() method and set its value into the "keepPlaying" variable.
So we go and execute the code in the keepPlaying() method (lines 45-57)
After the playAgain method control keeps going from this point.

line 13 - end of the loop - go back to the top of the loop (line 10) and check if "keepPlaying" is true. If it is go through again from line 11, otherwise exit the loop and keep going at line 14.

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am moving on to another program I found and trying to understand it. I am learning about both method and object classes. Here is the code :

I have two classes that go along with this program.
Ok, so I know that this is a main class. I also know that it launches another class from it and in turn launches a METHOD STARTGAME. Am I sort of correct so far?



Here is the next class (part of the code). Since the MAIN calls the Method StartGame in this class,
1) what does the Player p1; Player p2; Player p3; do?
2) why doesn't it skip it because startGame is below it?

PS - there is a third class called Player

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I return something out of a object class? If I created a random number in another object class, can I use that number in the main? Or do I just return what the method does in the object class?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

>1) what does the Player p1; Player p2; Player p3; do?

It declares three variables of type Player called p1, p2 and p3.
Because the are declared inside the class, but outside of any method they are class/instance variables.
They can be referred to from any method in this class.


>2) why doesn't it skip it because startGame is below it?

When you call startGame() it jumps straight to that point. So it does "skip" those lines when you call startGame().

Those lines of code actually take effect when you construct the GuessGame class - ie. with new GuessGame().
When you create a new GuessGame it
- loads the GuessGame class
- calls the GuessGame() constructor. If none is defined it calls the default constructor.
The constructor's job is to ready an object for use - i.e. make sure all its variables

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!