Skip to content

Commit 43a4c35

Browse files
author
Ram swaroop
committed
statics : initial commit
1 parent b707616 commit 43a4c35

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

_posts/2015-08-08-statics.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
layout: post
3+
title: Statics
4+
---
5+
6+
Static is one of the [non-access modifier](/2015/05/22/access-control.html) which has a huge role in Java. It can be used
7+
with both variables and methods. It is used when we want some variable or method not to be a part of any specific object
8+
of a class rather common for all objects of the class.
9+
10+
For example, keeping count of all the instances created for a particular class we can make the count variable `static` or
11+
when we have a method which returns the same result irrespective of the instance invoking it like `getRandom()` returning
12+
any random number or `getMax(int x, int y)` returning the larger number between `x` and `y` we can make the methods `static`.
13+
14+
### Ways to access static variables/methods
15+
16+
As static variable/methods doesn't belong to any particular object of a class, you need not create any object to access
17+
static variable/method. You can access directly with the help of classname and dot `'.'` operator but you can also access
18+
normally like any other instance variable/method i.e, via object reference and dot `'.'` operator. See below:
19+
20+
{% highlight java linenos %}
21+
class Frog {
22+
23+
static int frogCount = 0; // Declare and initialize
24+
// static variable
25+
int frogSize = 0;
26+
27+
public Frog() {
28+
frogCount += 1; // Modify the value in the constructor
29+
}
30+
31+
public static int getFrogCount() {
32+
return frogCount;
33+
}
34+
35+
public int getFrogSize() {
36+
return frogSize;
37+
}
38+
39+
public static void main(String[] args) {
40+
Frog f = new Frog();
41+
System.out.println(f.getFrogSize()); // Access instance
42+
// method via object reference f
43+
44+
System.out.println(Frog.frogCount); // Access static
45+
// variable via class
46+
47+
System.out.println(f.frogCount); // Access static
48+
// variable via object reference f
49+
50+
System.out.println(Frog.getFrogCount()); // Access static
51+
// method via class
52+
53+
System.out.println(f.getFrogCount()); // Access static
54+
// method via object reference f
55+
}
56+
}
57+
{% endhighlight %}
58+
59+
### Some points to remember here
60+
61+
1. Static method __cannot access__ a instance(non-static) variable.
62+
{% highlight java linenos %}
63+
64+
{% endhighlight %}
65+
66+
2. Static method __cannot access__ a instance method.
67+
{% highlight java linenos %}
68+
69+
{% endhighlight %}
70+
71+
3. Static method __can access__ a static variable or static method.
72+
73+
4. Static methods __cannot be overridden__. This is an important point. Consider the below program to understand:
74+
{% highlight java linenos %}
75+
class Animal {
76+
static void doStuff() {
77+
System.out.print("a ");
78+
}
79+
}
80+
81+
class Dog extends Animal {
82+
83+
// it's a redefinition,
84+
// not an override
85+
static void doStuff() {
86+
System.out.print("d ");
87+
}
88+
89+
public static void main(String[] args) {
90+
Animal[] a = {new Animal(), new Dog(), new Animal()};
91+
for (int x = 0; x < a.length; x++)
92+
a[x].doStuff();
93+
}
94+
}
95+
{% endhighlight %}
96+

0 commit comments

Comments
 (0)