Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 27, 2009

How to access a hidden variable in corejava

/**

* Write a program which illustrates how to access a hidden variable. class G

* declares a static variable named x of type int. class H extends G and

* declares an instance variable named x of type int. a display() method in H

* displays both of these variables.

*

* @author Ritesh

*

*/

public class Inher3 {

/**

* execution begin from here

*

* @param args

*/

public static void main(String[] args) {

// creating object of T class and display variables

System.out.println("Display Values Of Static Member Variables : ");

new H().display();

}

}

/**

* class H extends G and has an instance variable named x of type int.

*

* @author Ritesh

*

*/

class H extends G {

// member variable

static int x = 2;

/**

* display all the variables of G and H classes

*

*/

public void display() {

System.out.println("\tFrom Class H -> display() :");

System.out.println("\t\tsuper.x : " + super.x);

System.out.println("\t\tG.x : " + G.x);

System.out.println("\t\tx : " + x);

System.out.println("\t\tthis.x : " + this.x);

System.out.println("\t\tH.x : " + H.x);

}

}

/**

* class G declares an instance variable named x of type int.

*

* @author Ritesh

*

*/

class G {

// static member variable of type int

static int x = 1;

}

No comments:

Post a Comment

Followers