Click to See Complete Forum and Search --> : Global Varibale for Applet Instance


Deepika Sachdev
January 7th, 2000, 01:16 AM
In our Applet we are taking the Database connection in a variable in the Init function. It is required that all methods of the applet should be able to access this variable. Is there any method by which a variable can be made global only for that instance. I dont want to use Static as it will make the variable Global for all the instances of the Applet.

Chris Allen
January 7th, 2000, 06:19 AM
The best way is to make the variable private and write a getVarName function. Then for every creation of a new class pass an instance of the existing class to the constructor. e.g.

public class main {

private String var = "testing";

String getVar() {
return var;
}

main()
{
other test = new other(this);
}

}

public class other {

main mainClass;

other(main mainClass) {
this.mainClass = mainClass;
String var = mainClass.getVar();
}

}

This way you should be able to restrict access to the variable and make it semi-global.

Chris.

nanda
January 10th, 2000, 07:44 AM
I dont get ur problem like u can declare that conn variable as a class variable so that all the methods in an applet class can access that variable..... An instance of an applet will be created only when u call that html file as a separate process and even there init will be called again... do reply me if u hav any further doubts.....