Results 1 to 4 of 4
Thread: Static Methods
- 10-03-2011, 06:17 AM #1
Static Methods
From what I understand, not static methods can only be used directly by the instance under which the method exists. However, a static method can be accessed with the class name, not the instance name. Tell me if my understanding's wrong.
Now, I'm working through project euler (really great, check it out if you haven't) and all of the problems are solvable with one class that has a main() in it. However, I like to make methods to organize my thoughts, so I put some methods inside of the class. The compiler would only accept them if they were static, though. Why would that be? Why can't I make methods that are not static in this situation?
Here's a silly example I just made up:
If triple() isn't static, it won't compile. Why not? Shouldn't a method always be able to be called from within itself?Java Code:public class example { //main public static void main(String[] args) { int number = 1; System.out.println(triple(number)); } public static int triple(int n) { return 3*n; } }
Thanks
- 10-03-2011, 09:40 AM #2
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Re: Static Methods
It has to be main because you're calling it from the static main method. If you don't want it to be static, call it from the constructor like this:
Hope thats helpful.Java Code:public class example { public example{ System.out.println(triple(number)); } //main public static void main(String[] args) { int number = 1; new example(); } public int triple(int n) { return 3*n; } }Last edited by Guy; 10-03-2011 at 09:42 AM.
- 10-03-2011, 09:50 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
Re: Static Methods
No it isn't because your code doesn't even compile. A non-static method can only be called against a target object, i.e. the object that has to execute the body of that method. No object, no method; it's that simple. A static method needs a target, an object somewhere to call a non-static method against. If it hasn't a target object, it can't call that non-static method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 09:53 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Re: Static Methods
My bad, thanks for correcting me!
I guess I'm still to much of a newbie to be helping :P
This is what I meant to write though. which does compile:
public class numbers {
public numbers() {
int number = 1;
System.out.println(triple(number));
}
//main
public static void main(String[] args)
{
new numbers();
}
public int triple(int n)
{
return 3*n;
}
}Last edited by Guy; 10-03-2011 at 09:58 AM.
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Static and non static class methods question
By silverglade in forum New To JavaReplies: 2Last Post: 05-14-2011, 10:10 PM -
static methods
By billq in forum New To JavaReplies: 3Last Post: 12-30-2009, 02:34 PM -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks