Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Calling a method from within the same class

  1. #1
    gregpuzzles1 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Calling a method from within the same class

    I am new to the java programming language. This code is free of errors in Eclipse, what I am wondering is how to call the findOdds medhod from the main method. The findOdds method should return a list. I know in Python you could set it equal to a variable such as x = findOdds(100), I am just not sure how to do this in Java. Any help would be appreciated. Thanks.

    Java Code:
    import java.util.ArrayList;
    
    public class TestVoidMethod {
    	public static void main(String args[]){
    		System.out.println("Hello World");
    	}
    	public ArrayList<Integer> findOdds(int number){
    		ArrayList<Integer> odds = new ArrayList<Integer>();
    		for(int i = 3; i < number + 1; i += 2){
    			odds.add(i);
    		}
    		System.out.println(odds);
    		return(odds);
    	}
    	
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default Re: Calling a method from within the same class

    What happened when you tried?

    Note that you have to create an instance of a class before you can call its non-static methods.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gregpuzzles1 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Calling a method from within the same class

    All it does is print out the Hello World, I would like to put a statement below the Hello World line and call the findOdds method. Would something like findOdds newvar = new findOdds(100); work? The error I get when trying newvar is:

    Multiple markers at this line
    - findOdds cannot be resolved to a type
    Last edited by gregpuzzles1; 04-05-2012 at 07:24 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default Re: Calling a method from within the same class

    That's because findOdds is a method, not a type, so it really doesn't make sense to use "new" with it. The findOdds() method belongs to the type TestVoidMethod.

    Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calling a method from within the same class

    Quote Originally Posted by gregpuzzles1 View Post
    Would something like findOdds newvar = new findOdds(100); work?
    You just made that up didn't you? Programming, not jut in Java but any programming language, is not throwing a couple of keywords and method names, sprinkled with a few interesting looking parentheses together and hope for the best. Google for "Java Tutorials" (or click the link supplied by Kevin) and read them carefully; better yet: study them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gregpuzzles1 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Calling a method from within the same class

    Yes I did pretty much just make that up. (LOL) It appears that making an object out of the main class and then referencing that object with the method name will work:

    Java Code:
    TestVoidMethod method = new TestVoidMethod();
    System.out.println(method.findOdds(100));

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default Re: Calling a method from within the same class

    Quote Originally Posted by JosAH View Post
    You just made that up didn't you? Programming, not jut in Java but any programming language, is not throwing a couple of keywords and method names, sprinkled with a few interesting looking parentheses together and hope for the best.
    Unless you're talking about lisp...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calling a method from within the same class

    Quote Originally Posted by KevinWorkman View Post
    Unless you're talking about lisp...
    Lisp? Lisp? Kid stuff! You obviously have never seen Forth! Now that is a programming language for real men (like me)! ;-)

    kind regards,

    Jos
    KevinWorkman likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Calling a method from another class
    By andrewche in forum New To Java
    Replies: 5
    Last Post: 05-04-2011, 04:46 AM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM

Posting Permissions

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