I am trying to figure out different ways to call methods, and during experimenting, I have come up with 3 ways to do something.
public static boolean isFoo(int bar) {
return bar % 2 == 0;
}
//in a driver class:
foo.isFoo(7);
or
public static int foo;
public Foo(int foo) {
this.foo = foo;
public boolean isFoo() {
return foo % 2 == 0;
}
//in a driver class:
Foo foo = new Foo(4);
System.out.println(foo.isFoo());
or
public static int foo;
public Foo(int foo) {
this.foo = foo;
}
public static int getFoo() {
return foo;
}
public static boolean isFoo(Foo foo) {
return foo.getFoo() % 2 == 0;
}
//in a driver class:
Foo foo = new Foo(14);
System.out.println(Foo.isFoo(foo));
Which of these 3 ways are considered static and why? I'm not sure because they all use the word static, thus I believe that in the driver, I would be referring to all of them in a static context. Are they all static?
I have attempted googling but none seem to connect to my situation.
Thanks for your help.
You have only 2 static methods, first and third. Second one is instance method.
Coming to your actual question, do not over think. Keep the most clean and readable way. Perfect static method I would say here is
public static boolean isFoo(int bar) {
return bar % 2 == 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.