File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,21 +58,38 @@ class Frog {
5858
5959### Some points to remember here
6060
61- * Static method __ cannot access__ a instance(non-static) variable directly. They can access through a object reference.
61+ * Static method __ cannot access__ a instance(non-static) variable directly.
6262{% highlight java %}
6363public static int getFrogSize() {
6464 return frogSize; // compiler error
6565}
6666{% endhighlight %}
6767
68- * Static method __ cannot access__ a instance method directly. They can access through a object reference.
68+ But they can access through a object reference.
69+ {% highlight java %}
70+ public static int getFrogSize() {
71+ Frog obj = new Frog();
72+ return obj.frogSize; // ok
73+ }
74+ {% endhighlight %}
75+
76+ * Static method __ cannot access__ a instance method directly.
6977{% highlight java %}
7078public static int getFrogSizeSum() {
7179 int size = getFrogSize(); // compiler error
7280 return size * frogCount;
7381}
7482{% endhighlight %}
7583
84+ But they can access through a object reference.
85+ {% highlight java %}
86+ public static int getFrogSizeSum() {
87+ Frog obj = new Frog();
88+ int size = obj.getFrogSize(); // ok
89+ return size * frogCount;
90+ }
91+ {% endhighlight %}
92+
7693* Static method __ can access__ a static variable or static method.
7794
7895* Static methods __ cannot be overridden__ . This is an important point. Consider the below program to understand:
You can’t perform that action at this time.
0 commit comments