• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Constructor Calling

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Every one
I saw this phenomenon as i was explaining some java code about multithreading to my friend today.
If i write a class A and class B with default access specifiers, I can call the construtor of one of the class in the other by writing "new" before it. Here is what I did to check it out.
class A
{
A()
{
system.out.println("Inside class A");
}
dummy()
{
system.out.print("dum dum");
}
}
class B
{
new A(); //calling the constructor!!!
}

So that is the code and i would be very greatfull if i can get and explanation about it from someone.
Regards
AbdulWajed
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this question has nothing to do with Performance, I am going to move this discussion to Java( intermediate ) forum.

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your code as you have written it will not even compile. Therefore an explanation of how it will run will be very short.
You can only place member variable definitions, methods, static blocks, or other class definitions inside any class! What you have placed there: new A() doesn't fit any of the correct Java items that can be placed there.
If you want to you can say: A a = new A();
Of course, in that case we have Java 101: basically the has-a relationship.
Puzzled by question,
Manfred.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have
class B extends A{
B(){};
}
It will call the no args constructor in A.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic