I have declared a function in a class and want to know if I could define it outside the class. I have seen these cases in C++ but haven't come across anything like this in Java. Does Java allows doing this?
- Everything in Java needs to be inside a classCory Roy– Cory Roy2015-09-18 22:57:59 +00:00Commented Sep 18, 2015 at 22:57
- @CoryRoy interface too? :PNir Alfasi– Nir Alfasi2015-09-18 22:58:22 +00:00Commented Sep 18, 2015 at 22:58
- @alfasin You are correct, class or interface.Cory Roy– Cory Roy2015-09-18 23:00:29 +00:00Commented Sep 18, 2015 at 23:00
- @alfasin enum also,trolling :DKumar Abhinav– Kumar Abhinav2015-09-18 23:03:56 +00:00Commented Sep 18, 2015 at 23:03
- @KumarAbhinav if you want to be a real wiseass you should say that it doesn't apply to a... class, as well :)))Nir Alfasi– Nir Alfasi2015-09-18 23:20:58 +00:00Commented Sep 18, 2015 at 23:20
Add a comment |
3 Answers
I have declared a function in a class and want to know if I could define it outside the class. I have seen these cases in C++ but haven't come across anything like this in Java. Does Java allows doing this?
No, you can't do that outside a class. This is similar as asking whether we can write some codes without a class at all. Everything is done within a class in Java.
If no class exist, then at least it will be an interface.
Example:
class{ void method(){} } interface{ void methodA(); default void method(){} }