Results 1 to 8 of 8
- 04-05-2012, 07:13 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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); } }
- 04-05-2012, 07:15 PM #2
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!
- 04-05-2012, 07:21 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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 typeLast edited by gregpuzzles1; 04-05-2012 at 07:24 PM.
- 04-05-2012, 07:35 PM #4
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!
- 04-05-2012, 09:09 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Calling a method from within the same class
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-05-2012, 09:13 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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));
- 04-09-2012, 02:31 PM #7
Re: Calling a method from within the same class
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-09-2012, 02:45 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Calling a method from within the same class
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Calling a method from another class
By andrewche in forum New To JavaReplies: 5Last Post: 05-04-2011, 04:46 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks