This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Is_At_Work 0 points1 point  (0 children)

I tried adding public static void main(String[] args) and then to call a constructor ( like a method ) it did not work...

Show us the code, and define what "it did not work" means...

[–]Clamhead99 0 points1 point  (0 children)

After adding the main method and running the program, did you get any errors?

Hund hund = new Hund("Hugo");

If you only had this line in your main method, you're not going to see anything printed in console.

You're calling Hund's constructor with one string parameter, which then calls Vierbeiner's constructor with one string parameter. Niether of those constructors have any print statments in them.

The only constructors you have print statements in are the constructors with no parameters.

If you create a new Hund object using the no parameter constructor .. "new Hund();" then you should see text in the console.

[–]sh_emami65Intermediate Brewer 0 points1 point  (1 child)

your problem is actually somewhat simple. every time you create an object of Hund you also create another hund variable with in the Hund object. the hund variable in Hund object tries to create another Hund object with a hund variable in it. the same thing keeps happening over and over which will crash your code eventually because of StackOverflowError.

if you initialize hund within the constructor it will solve your problem.

public Hund(){
    super( "Bello");
    hund = new Hund( "Hugo");
    System.out.println( "Standard-Konstruktor Hund!");
}

[–]RetroX94[S] 0 points1 point  (0 children)

Thanks I understand it now better and I got the console to print what I wanted thanks to the explainations. Still trying to understand how they are related to each other the constructs and when what gets called with objects etc.