SRL Archive - Java Help and Tutorials

What am I doing wrong?


weequ

2009-12-20 05:59:26

public class HelloWordProjekti {


public static void main(String[] args) {
int I = AddFive(6);
System.out.println(I);
}

public static int AddFive(int x) {
Return x+5;
}

}
And if anyone could explain the difference between static and not static methods… ;)

Baked0420

2009-12-20 08:00:44

Java is case-sensitive, change your Return x+5; to return x+5;. And I prefer to do return(x+5); I think it looks better having what it returns in ()'s. And you don't need a variable for int I = AddFive(6);, you can just do System.out.println(AddFive(6));

but good luck learning java, it's fun. If you need more help you can PM me.

Gu3st...

2009-12-20 08:21:06

addFive would be more correct too by the way, instead of AddFive

weequ

2009-12-20 08:26:27

Thanks a lot guys. I hope it works and Ill post if it doesnt ;)
Edit: Works!! :DD

Baked0420

2009-12-20 10:46:05

yep, it should, I tested it :p

and yea, you usually name methods starting with lowercase, then go uppercase for each new word, and you usually name variables starting lowercase also, so people would normally do int i, as opposed to int I, but it's all the same, it still works regardless.

arash

2009-12-20 11:22:04

Static methods are when you don't need instance variables to function. Examples of this would be math.random, parseint, etc.

super_

2009-12-20 19:34:47

static methods are for when you don't need to operate on object state… i.e. is stateless. they should only really be used for utility methods most of the time, or else you're programming more procedural than object oriented.