Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the potential problem?  RSS feed

 
xiaorui chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code, without the super() called in the constructor. It seems runs fine.
What could be some potential problem it could have?
public class A extends Thread {
public JavaMailer() {
//super();
this.start();
}
public void run() {
...
}
}
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could be wrong here, but it should run just fine. You don't need to call the super() method inside a constructor. You override run() which is the thing you need to do, so it should be fine. The only thing you are not doing is allowing your threads to have names. So I would create two constructors, one being the default that calls super(), and another being a construcotor that allows you to pass in a String and then call super (string); so you can give your threads a name. Could become useful for debugging purposes.
 
xiaorui chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue here is that without calling super(), the constructor of the superclass Thread would not be called. In the Thread's constructor, it calls init() function which relates security issue. I think this should matter. But I have not run into problem regarding that yet. I do not know why.
 
xiaorui chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by xiaorui chen:
The issue here is that without calling super(), the constructor of the superclass Thread would not be called. In the Thread's constructor, it calls init() function which relates security issue. I think this should matter. But I have not run into problem regarding that yet. I do not know why.
Could the following error be resulted in some senario?
at java.security.AccessControlContext.optimize(AccessControlContext.java,
Compiled Code)
at java.security.AccessController.getContext(AccessController.java, Compiled
Code)
at java.lang.Thread.init(Thread.java, Compiled Code)
at java.lang.Thread.<init>(Thread.java, Compiled Code)


 
Patrick Mey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not specified super() is always called. You only need the call the constructor of the superclass explicitly when parameters are needed.
E.g. super("ThisIsMyThread") create a Thread with the name : "ThisIsMyThread".
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xiaorui,
See JLS2 8.8.5 and 8.8.7 for the full scoop.
jply
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!