Results 1 to 18 of 18
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Question on methods

    Hi I've been following along with tutorials on Oracle's page and I've been learning about methods. I had a question. I thought methods were a code that you wrote and then you can name it and it will be repeated, but I try to make a method called hey that prints out hey, and it only does it once, even though I named it four times. Why does this happen? If I take out the return from where it is, or put it elsewhere, it says:"Return statement missing" or "Return value not specified", respectively. Why is this happening? Also, a question about methods itself: since the things that go in parentheses are parameters, why do you need to put them? Why not just leave the parentheses blank? Aren't you limiting your options when you put in parameters? I may be wrong, as I dont quite understand parameters. (Sorry for the rogue question, but I didn't want to make another thread just for it.) Anyway, here's the code.
    Java Code:
    public class method {
    public static void main(String[] args) {
    }
    char hey()
         {
    System.out.println("Hey");
    return hey();
        }
    {
         hey();
       hey();
     hey();
       }
    }
    Last edited by MW130; 01-07-2013 at 12:14 AM.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    It was a little hard to see where you defined your method. You defined it as

    Java Code:
    char hey(){
       System.out.println("Hey");
       return hey();
    }
    Do you know what the char is in char hey()? You do not always need parameters because you do not always need to pass anything to the method. A method is a block of code that may be repeated, but not always. Sometimes it makes it easier to break up a program into logical parts.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    what does "passing something" mean? Yes, the char is "hey". Thanks for your reply.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Oh and another question, do you have to use a method? If you don't, can you just say the class and then go straight into like declaring values and all of that? I mean, apart from the main method.

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    You can put all your logic into the main method. However, it will get messy and impossible to read. I will go ahead and tell you a little bit about methods. When you declare (Create) a method, you have to include certain parts in naming it.

    Java Code:
    public static void main(String[] args){
    //Code goes here
    }
    This is an example of a method, in this case it's the main method. You see how it is named?

    public - Who can access it, don't worry about this too much right now
    static - Association with the class or an object. Don't worry about this either
    void - The return type. More on this in a bit
    main - The name
    () - Parameters go in here
    String[] args - The actual parameters, an array of Strings.

    The void is important, and seems to be what you are asking about right now. When you have void written there, it means that you don't have to return anything. There are different return types for example

    Java Code:
    int, String, char etc..
    So, if you wanted to say print out a string you could do this.

    Java Code:
    public static void main(String[] args)}
       String helloString = sayHello("Bryan");
       System.out.println(helloString); 
    } 
    
    public static String sayHello(String name){
         return "Hello " + name;
    }

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    hey, I tried to make a simpler example of the problem i'm having. This time, it says that it doesnt recognize when I try to run the method :(.

    Java Code:
    class rec{
    int height;
    int width;
    void rect(){
    int area = height * width;
    System.out.println("The area is " + area);
      }
     }
    class rekt {
    public static void main(String[] args) 
    {
    rect();   // the problem is here, it says this is not a recognized symbol. I am trying to run the method that I previously made.
      }
    }

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Thank you so, so much for the reply. It truly helped. But the thing is, now I'm confused on what return does. I thought it just ended the code...? what does static do? also, now that you have that method, how do you run it again, later on? Last thing: Does the main method being void mean that you cannot return values in it? Can you please explain a little more what return does? I can't find a proper tutorial to explain it :(. I express the deepest gratitude for your help, you are awesome :) Thanks, Michael

  8. #8
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    Quote Originally Posted by MW130 View Post
    hey, I tried to make a simpler example of the problem i'm having. This time, it says that it doesnt recognize when I try to run the method :(.

    Java Code:
    class rec{
    int height;
    int width;
    void rect(){
    int area = height * width;
    System.out.println("The area is " + area);
      }
     }
    class rekt {
    public static void main(String[] args) 
    {
    rect();   // the problem is here, it says this is not a recognized symbol. I am trying to run the method that I previously made.
      }
    }
    Because you declared them in two different classes, they cannot see each other.

    Quote Originally Posted by MW130 View Post
    Thank you so, so much for the reply. It truly helped. But the thing is, now I'm confused on what return does. I thought it just ended the code...? what does static do? also, now that you have that method, how do you run it again, later on? Last thing: Does the main method being void mean that you cannot return values in it? Can you please explain a little more what return does? I can't find a proper tutorial to explain it :(. I express the deepest gratitude for your help, you are awesome :) Thanks, Michael
    Return does almost exactly as it says. It returns a value, and yes it does end the portion of code.

    Java Code:
    public int returnFive(){
      return 5;
      System.out.println("We printed Five!"); 
    }
    The above wouldn't even compile because the print statement is never reached. When you return the number 5, the method returnFive ends. I would not worry about what Static does until you have a firm grasp of the basics and when you start to learn about objects. I don't want to raise more question in trying to solve these just yet. You can run the method repeatedly by calling it, again and again.

    Java Code:
    public static void main(String[] args){
       doSomething();
       doSomething(); 
       doSomething()
    } 
    
    public static void doSomething(){
      System.out.println("I did something!");
    }
    Yes, because the main value is return, it does not return anything. Return returns the value to the method that called it. In your case, it's the main method. There is nothing for the main method to return to, so therefore it is void.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Guys, I got it! I was calling it wrong. You have to make an object using classname objectname = new classname() so that you can call the method, then you do .

    the code is:


    class rec{
    int height=5;
    int width= 10;
    void rect(){
    int area = height * width;
    System.out.println("The area is " + area);
    }
    }
    class rekt {
    public static void main(String[] args)
    {
    rec rk = new rec();
    rk.rect();
    }
    }

    for anyone that is interested. Thanks all. I just wish you could call the method without making a new object... :(

  10. #10
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    You can, just put them into the same class

    Java Code:
    public class rec{
    
    static int height=5;
    static int width= 10;
    	
    
    public static void main(String[] args){
    	rect();
    }
    
    
    public static void rect(){
    		int area = height * width;
    		System.out.println("The area is " + area);
    }
    
    
    }

  11. #11
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Hey, thanks. Why do you put rect() in to main method?

  12. #12
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    Quote Originally Posted by MW130 View Post
    Hey, thanks. Why do you put rect() in to main method?
    That would be because I needed to call the rect method. All Java programs start from the main method, so when I ran the program I ran the main method and thus the rect method.

  13. #13
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Ok. is it possible to leave the main method blank? Also, when you are not using method, you write all your code inside the main method, right? Can the main method be repeated? Thank you!

  14. #14
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    No it can't be blank. I want you to tell me why. Make a program if you have to. The main method can't be called from anywhere, so it cannot be "repeated". You could however duplicate the code.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Question on methods

    Quote Originally Posted by Wnt2bsleepin View Post
    The main method can't be called from anywhere
    Wrong. What made you think so?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on methods

    Wait, so it can or it can't? Im so confused...

  17. #17
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Question on methods

    Quote Originally Posted by DarrylBurke View Post
    Wrong. What made you think so?

    db
    I knew I would make a mistake somewhere. I was able to call the main method from inside another method. I just have never used it.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Question on methods

    There's nothing special about the main(...) method except that the JVM invokes it with the command line parameters as an entry point to the program: Chapter*12.*Execution

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Question about arrays and methods
    By BenH in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 02:45 AM
  2. Question on HashTable, and methods.
    By fatabass in forum New To Java
    Replies: 16
    Last Post: 01-30-2012, 03:13 PM
  3. Question concerning methods
    By jim01 in forum New To Java
    Replies: 6
    Last Post: 05-18-2011, 02:46 PM
  4. 2 methods with same name Question
    By dienesh77 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 01:58 PM
  5. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •