Beginner's questions

Bonjour,

Maybe I'm trying to learn Java not from the best source but I'm already lost at the beginning of the tutorial.

First, I don't understand why I need to give the class name twice:

Puppy myPuppy = new Puppy( "Tommy" );

There was no explanation for this. My logic says myPuppy = new Puppy("Tommy") should be fine. What's that other Puppy at the beginning?

Second question:

public int getAge( ) {
   System.out.println("Puppy's age is :" + puppyAge );
   return puppyAge;
} 

This is also confusing. getAge as above should just return an integer. Or, it should be void and print only (no return). What's the point mixing these two?

Any help appreciated.

Comments

    1. How will the compiler know what type of variable it is? It's the same way as defining integer or any other variable.

    2. You are correct, the print statement has no place in get methods.

  • Thanks so much. I meant, since it is declared as a "new Puppy", what else could it be other than Puppy? Seems redundant for me however I understand declaration syntax has to start with the type.

  • It's the same as arrays, keeping it uniform:

    Type[] name = new Type[5];
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories