Results 1 to 4 of 4

Thread: questions

  1. #1
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default questions

    when we are creating a new class, and we want to make use of a lot of methods, we do something like this? (we put all the methods inside the main method? or only the void methods?)

    Java Code:
    public main method (){
    method2();
    method3();
    }
    
    public void method2(){
    blah blah blah}
    
    public method3 (){
    blah blah blah
    return value;
    }
    how can we use in a (return) method a variable that is included in a previous (void) method?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class MethodsTest {
        public static void main(String[] args) {
            doSomething();
            String s = getSomething();
            String retVal = doSomethingElse(s);
            System.out.println("retVal = " + retVal);
        }
    
        private static void doSomething() {
            System.out.println("doingSomething");
        }
    
        private static String getSomething() {
            return "something";
        }
    
        private static String doSomethingElse(String s) {
            return "somethingElse " + s;
        }
    }

  3. #3
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    you are fast thanx


    if i wanted to return the ''somethingElse '' to the method getSomething, i would write this
    Java Code:
     String retVal = doSomethingElse(s);
            System.out.println("retVal = " + retVal);
    as it is inside the getSomething method (and no reference of it in the main method)?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    A method can call other methods.
    Java Code:
    public static void main(String[] args) {
            String s = getSomething();
            System.out.println("s = " + s);
        }
    
        private static String getSomething() {
            String s = getSomethingElse();
            return "something + " + s;
        }
    
        private static String getSomethingElse() {
            return "somethingElse";
        }

Similar Threads

  1. Stuck on Two Questions, Please Help
    By sylo18 in forum New To Java
    Replies: 5
    Last Post: 03-11-2008, 01:03 AM
  2. 2 simple java questions
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 09:35 AM
  3. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM
  4. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

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