• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in my code?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Emp
{
private String name;
Emp(String str)
{
name = str ;
}
Emp()
{
this("hi"); //Why this code is not executing?
}
}
class Mgr
{
Emp e1 = new Emp();
Emp e2 = new Emp();
Mgr()
{
System.out.println("print");
}
}
class StaticEmp1
{
public static void main(String arr[])
{
Mgr m1 = new Mgr();
}
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is executing. What makes you think it isn't?
Try putting a print statement in the code you think isn't executing, to see what's happening.
[This message has been edited by Jim Yingst (edited March 17, 2000).]
 
Manju Swamy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Could you please state you question clearly.

  • Did you had error while compiling? It compiles perfectly fine for me.
  • Are you expecting the this("hi") to print hi in the constructor? You need to refresh your constructor chaining concepts.
  • Do you have all these classes in one single source file?


  • In case you have all these classes are in one single file like Emp.java or Mgr.java, when try to compile and run from the IDE, since main() method is not defined in Mgr or Emp classes you will get a runtime error.
    In case you have successfully compiled the classes then try running StaticEmp1 class from the command prompt.



    [This message has been edited by Manju Swamy (edited March 17, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic