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.
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);
}
}
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.
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
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)
Re: Calling a method from within the same class
Quote:
Originally Posted by
gregpuzzles1
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
Re: Calling a method from within the same class
Yes I did pretty much just make that up. :(giggle):(LOL) It appears that making an object out of the main class and then referencing that object with the method name will work:
Code:
TestVoidMethod method = new TestVoidMethod();
System.out.println(method.findOdds(100));
Re: Calling a method from within the same class
Quote:
Originally Posted by
JosAH
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...
Re: Calling a method from within the same class
Quote:
Originally Posted by
KevinWorkman
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