About creation


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
class Dog{

int age;

public Dog(int dogsAge){

age=dogsAge;
}
public static void main(String[] args) {

Dog spike(3);
}

}

<In what way does your code behave incorrectly? Include ALL error messages.>
Dog spike(3);
Why i must code:
Dog spike=new Dog(3);

```

Replace this line with your code.

<do not remove the three backticks above>

#2

Because in Java all declared variables must have a class or type. We cannot simply write,

spike = new Dog(3)

because unlike JavaScript, variables do not inherit their type from the assigned object. It must be explicitly declared. Once Java sees that the variable type matches the class being assigned to it, it permits the assignment.


#3

Thanks very much
I learnt c++ before,so i am not used to use new.I understand now.

—Original—


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.