Question on how this works


#1



First time posting wasn't sure what to title it or where to put it so I will apologize in advance for any mistakes.
I came across something interesting/confusing and wasn't sure why its doing this I would greatly appreciate an explanation if anybody has one. Thanks in advance I tried to make things as clear as possible.

In my constructor if i put int age = dogsAge; instead of age = dogsAge; have public int getAge() { return age; } and then have Dog spike = new Dog(5); then int spikeAge = spike.getAge();
System.out.println(spikeAge); inside main it results in printing 0 instead of what i set the age to in the constructor.
Just to clarify things if i don't put int age = dogsAge; and instead put age = dogsAge; removing the Int it will work as expected printing out whatever I set dogsAge to when I create a new dog.


class Dog {

//Variables
  int age;

//Constructor
  public Dog(int dogsAge) {
  int age = dogsAge;
	}  
//Method Definitions
  //Bark
  public void bark() {
    System.out.println("Woof!");
  	}

//run
  public void run(int feet) {
    System.out.println("Your dog ran " + feet + " feet!");
  	}

//getAge
  public int getAge() {
    return age;
  	}
//main
public static void main(String[] args) {

  Dog spike = new Dog(5);
  spike.bark();
  spike.run(10);
  int spikeAge = spike.getAge();
  System.out.println(spikeAge);
	}
}


#2

Hi there.

In your constructor, when you say int age = dogsAge; you're declaring a separate age variable. It's separate because you used the keyword int to denote that it's a new integer. And the reason why the names don't clash is because they're not declared in the same scope as one another.

Scope, if you're unsure, just means where the methods and variables reside in relation to each other. Eg, Class Dog is the main scope where everything inside of it is sort of like a family. Int age can be accessed anywhere because it is declared at the class level. public void bark() is a scope of its own. It's part of the class dog but inside of the bark(), variables that are declared are only available to use inside that scope. Other methods like run() or getAge() will not have access to these variables unless you pass it in a method call parameter or move the variable to the outer class Dog scope for everyone to have access to.

So because they are separate the age declared in the constructor is set to 5 and shortly after is destroyed because nothing is actually done with the variable.

The class level variable is declared as int age;. Declaring variables without defining their values explicitly ie not int age = 1; will assign that variable a default value automatically. So int age; Will essentially be equal to 0.

And because whatever is in the constructor is not reflected in the class age variable that is why when you call spike.getAge() you're getting 0.

Hope that clears it up.


#3

Thanks for the clarification I didn't even think about the scope and since they never really went into any of the fundamentals of what each thing is doing there wasn't really a way for me to fully understand it and for me I can't really retain anything I learn if I don't understand it. Memorization vs Understanding you know


#4

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