Discussion Locked

java 2.0 programming (URGENT)

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

What is "STATIC" & "THIS" concept in JAVA 2.0

Explain with example.

7 replies

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 chaosruled

Check in the Downloads section of TechRepublic. Look for the terminology download. This will help you to understand the jargon a little better.

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

This discussion is closed to new replies.