Why does dogAge return value 0?


#1
class Dog {
  
  int age;

	public Dog(int dogsAge) {
    
  	dogsAge = age;
    
  }
  
  public void bark() {
		
    System.out.println("Woof!");
    
	}
	
  public void run(int feet) {
    
    System.out.println("Your dog ran " + feet + " feet!");

	}
  public int getAge(){
    return age;
  }
  
	public static void main(String[] args) {
    
    Dog spike = new Dog(5);
    spike.bark();
    spike.run(40);
    
    int spikeAge = spike.getAge();

    System.out.println(spikeAge);
	}

}

#2

you define age here:

int age;

but then you never give it a value, so i assume 0 is the default value given by java.


#3

public Dog(int dogsAge) {

dogsAge = age;

}
Dog spike = new Dog(5);
doesn’t change the value of age?


#4

if done right, yes, but not at the moment. You assign the value stored in age to dogsAge

when we create a new dog:

Dog spike = new Dog(5);

the constructor (Dog) gets called:

	public Dog(int dogsAge) {
    
  	dogsAge = age;
    
  }

so 5 is passed to the parameter(dogsAge) of constructor, so what should we do inside the constructor then?

you can perfectly add System.out.println() inside constructor to see values of variables


#5

ohh… so here dogAge get equal to age but it won’t change the value for age?


#6

get equal i always find such a vague term. Example:

int x = 5

i much rather say: we give x a value of 5 or assign a value of 5 to x

But yes, you assign dogAge a value of age, but why would you want to do that? Do you think that is right?


#7
public Dog(int dogsAge) {

dogsAge = age;

}
this is part of instruction from lesson.

I am learning Java for 1 week after complete quick course about javaScript.
as of now i feel java is so much like mixing of things and mess. hopefully slowly it gets clear.


#8

Took me a while, i found the lesson in question:

https://www.codecademy.com/courses/learn-java/lessons/object-oriented-programming/exercises/classes-constructor-parameters?action=resume_content_item

the instructions say:

Inside of the constructor block, set the instance variable age equal to the dogsAge parameter

what you did:

set dogsAge parameter equal to instance variable age

we want to assign the parameter value to the instance variable, this way when we create a dog, we can have a different age for all the dogs.

you never answered this question, understanding why and what you are doing is far more important then getting to the solution


#9

the dogsAge is assigned the value of Age in Dog method because they are related ?

this is what made me confuse from beginning . could you please explain.


#10

what do you define as related? Blood related?

age is an instance variable, it has a different value for every instance of Dog class. Here:

Dog spike = new Dog(5);

you create an instance of class Dog named spike

The way we achieve different values for each instance is by using the constructor, here is your constructor:

	public Dog(int dogsAge) {
    
  	dogsAge = age;
    
  }

when we create an instance (like spike), the value(s) we supply as argument (5 in your case) get passed along to the parameter (dogsAge) of the constructor, so we can set a value for the instance variable (age)


#11

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