This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
java_class [2016/09/09 13:55] 178.237.8.52 |
java_class [2016/09/09 14:01] (current) 178.237.8.52 |
||
---|---|---|---|
Line 292: | Line 292: | ||
public | public | ||
- | WHAT???? This has to do with the cumbersome subject of [[method hiding]] that happens with static methods, as ''main'' in this case. There are 2 things to notice: | + | WHAT???? This has to do with the cumbersome subject of [[method hiding]] that happens with static methods, as ''main'' in this case. |
- | * the output is exactly the same as the output of ''java package1.SubClass'' | + | Static methods/variables are a unique property of the class and can live without an instance of it. Because of this, [[http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.2 | with static methods the actual code to be run is decided at compile-time and only the type decides the code to be run]]. |
- | * when we changed the ''static'' method ''main'' in ''AnySubClass'' we could only access to ''public'' and ''protected'' variables, | + | |
- | + | ||
- | In practice Java is protecting your code! Static methods/variables are a unique property of the class and can live without an instance of it. Because of this, [[http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.2 | with static methods the actual code to be run is decided at compile-time and only the type decides the code to be run]]. | + | |
In a case like this one, in ''AnySubClass2'' we inherited the ''static'' method ''main'', and because it is defined in the type ''SubClass'', Java uses the implementation of the method in the ''SubClass'' (together with the variables in there, that are the only known variables to the method in ''SubClass''). | In a case like this one, in ''AnySubClass2'' we inherited the ''static'' method ''main'', and because it is defined in the type ''SubClass'', Java uses the implementation of the method in the ''SubClass'' (together with the variables in there, that are the only known variables to the method in ''SubClass''). | ||
+ | In practice Java is protecting your code! | ||
<code java SubClass.java> | <code java SubClass.java> |