How to call a value to another


#1
class Dog {
  int age;

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

  public static void main(String[] args) {
    Dog spike = new Dog(5);
  }    
}

#2

@hillrm can you clarify your question?


#3

Hey @hillrm. Pls clarify your question. But if you said how to call a value to another method. You will have to use the publicize the variable :laughing: .

Meaning this is how you do it(Instead i will just publicize age for you): So instead of your code :

Do: public int age;

Hope this helped!


#4

@amanuel2 it is good practice to add modifiers to class fields but
In the spirit of encapsulation, it is common to make fields private.
This means that they can only be directly accessed from the within class.
It is still pertinent that we access to these values, however. This can be done indirectly by adding public methods(accessors and mutators) that obtain the field values.
Read more here on Oracle