Results 1 to 3 of 3
Thread: Need help
- 03-24-2011, 06:21 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
- 03-24-2011, 08:31 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
see the below code
Java Code:public class RegexTest { int i = 0; //static int i = 0; public static void main(String[] args) { RegexTest obj1 = new RegexTest(); RegexTest obj2 = new RegexTest(); RegexTest obj3 = new RegexTest(); obj1.myMethod(); obj2.myMethod(); obj3.myMethod(); } private void myMethod() { System.out.println("i value while entering = "+i); i++; System.out.println("i value while leaving = "+i); } }
I have commented one line, a static integer i.
First you execute this code and see the result. Then comment int i = 0 and uncomment static int i = 0 and see the result. Compare both. Then you'll understand why static variable :)
Meanwhile I'll post about static methods
- 03-24-2011, 08:47 AM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Hope you got the concept of static variable.
Now static method is independent of instance. It is class specific.
To access a static method you should use the class name, the dot operator and the method name.
There are other restrictions also,
a static method cannot access non-static variable (instance variable) directly.
a static method cannot be over-ridden
Bookmarks