Contructor methods not being called

Put your problem here if it does not fit any of the other categories.

Contructor methods not being called

Postby Schermvlieger » Fri Mar 19, 2010 9:28 am

Hi guys/gals,

I am so sorry to post this here, but i am really stumped and don't know where else to ask. I have read and reread the parts about creating and instantiating classes on http://java.sun.com/docs/books/tutorial/index.html but it looks like I am doing it right. I am new to this programming language though.

The problem:

The constructor methods of my inner classes are not being called. Is this something Android specific? I cannot find anything about that in the Dev Guide, and it seems rather unlikely that this is intentional behavour.

This is the relevant part of the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class MyActivity extends Activity {
  5.  
  6. Thing mything;
  7.  
  8. public void onCreate(Bundle savedInstanceState) {
  9.  
  10. mything = this.new Thing()
  11.  
  12. }
  13.  
  14. private class Thing {
  15.  
  16. public void Thing() {
  17.  
  18. // initializing stuff here
  19.  
  20. }
  21.  
  22. }
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


When I run this code, the constructor method of mything is not executed. I have to place an explicit call to Thing() in OnCreate to have my initialization code to run. What am i doing wrong here?
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Top

Re: Contructor methods not being called

Postby pskink » Fri Mar 19, 2010 9:45 am

Schermvlieger wrote:This is the relevant part of the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyActivity extends Activity {
  3. Thing mything;
  4. public void onCreate(Bundle savedInstanceState) {
  5. mything = this.new Thing()
  6. }
  7. private class Thing {
  8. public void Thing() {
  9. // initializing stuff here
  10. }
  11. }
  12. }
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


When I run this code, the constructor method of mything is not executed. I have to place an explicit call to Thing() in OnCreate to have my initialization code to run. What am i doing wrong here?


create constructor without void


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. private class Thing {
  5.  
  6.         public Thing() {
  7.  
  8.                 // initializing stuff here
  9.  
  10.         }
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby Schermvlieger » Fri Mar 19, 2010 10:08 am

Thanks pskink!

So typical of me to overlook these little errors when I lack routine in something >.<
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests