Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem wtih protected method with global variables

Status
Not open for further replies.

thumpkidd9

Programmer
Mar 27, 2005
42
US
Ok. I am extending the class HttpServlet and using the method
Code:
protected void doGet(...) {}
in that method, I called another method called
Code:
static void parse_Log()
that populates a 2-d array called list. I initialize this globally like so.
Code:
static String list[][] = new String[list_size][ele_size];
When I run the program, any variable that I initialized globally is null or 0 in the doGet() method. Is there anyway I can get access to these variables from the protected method? Do I have to declare them differently?

Thanks
 
If you want some comde to be run at the creation class time, you can include them in the constructor or inside an static declaration.

Code:
public class someClass {

static {
// initialization code
}
public someClass() {
 // initialization code
}

}

Cheers,

Dian
 
Ok, There is no main in my class. Can I still have an contructor for it?

 
Wouldn't I still have to declare the variables globally, then initialize them in the contructor? If I declare globally it doesn't work, and if I dont declare them, I get an error saying the compiler doesn't recognize the variables.
 
Generally, the doGet and doPost methods have public access in your extended classes.

If you want to initialize globals, then override the init() method - this is just called once when the servlet is constructed.

Bear in mind that globals will NOT be thread safe - you will need to synchronize access if you want thread-safety.

--------------------------------------------------
Free Database Connection Pooling Software
 
what is the init() method? I am sorry but I am confused. It doesn't seem that doGet has public access to the static variables because they print fine inside static void parse_Log but not in protected void doGet(). I've tried making a constructor and initializing them there but I am not sure on teh correct syntax. Inside the protected method I create an object from the populate class
Code:
String list[][] = new String[][]
Populate p = new Populate(list);

constructor looks like

Code:
public Populate(String[][] list) {
  list1 = new String[list_size][ele_size];
}

should I call parse_Log() from inside the constructor?
 
I dont think I can use the constructor, I get an
InstantiationException when I try and create an instance of Populate which is an Abstract class I guess.
 
The init() method is provided by the GenericServlet class which HttpServlet extends. It is called when the servlet is 'brought into service' and is there for you to override in your class.

This is written plainly in the J2EE API docs, BTW.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top