Skip to content

Commit dc77169

Browse files
author
Ram swaroop
committed
statics : updated
1 parent c8816fd commit dc77169

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

_posts/2015-08-08-statics.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff 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 %}
6363
public 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 %}
7078
public 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:

0 commit comments

Comments
 (0)