java 2.0 programming (URGENT)
What is "STATIC" & "THIS" concept in JAVA 2.0
Explain with example.
Web Development
What is "STATIC" & "THIS" concept in JAVA 2.0
Explain with example.
by mightyduk
static is a modifier to a variable or function definition. It indicates that the variable/function is a class level item, and not tied to an instantiated object.
this is a reference to the object for which the function was called. ie if i call:
a.thisfunc()
Object thisfunc() { return this;}
another reference to the "a" object will be returned.
by saurabhazad
by chaosruled
by saurabhazad
by venkats
A 'static' variable / object reference retains the same value through all the instances of the containing class.
Ex: class class1 {
static int a;
}
class1 c1, c2, c3 ;
c1.a = 123;
// now the value of a in c2 and c3 also will be 123, and changing its value in any of the instances of class 'class1' will be reflected in all the other instances.
A 'static' function can be referenced with instanciating the container class.
Ex: class class1 {
public static void func1 () {....}
public void func2 () {...}
}
class1.func1 () ;
// this is valid since func1 () is static.
class1.func2 ();
// this is invalid, func2 () requires an object of class1 to instanciated before referring to the function.
----------------------------
'This' reference points to the 'current' object context. When a line of code in a class refers to 'this', it refers to the current object at the run-time.
Ex: class class1 {
int a;
public void function fun
by saurabhazad
by saurabhazad
This discussion is closed to new replies.
Sort by Trending