Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Java example to create class to set, get name and age of a puppy using constructor

public class Puppy {
 int puppyAge;

 public Puppy(String name) {
  // This constructor has one parameter, name.
  System.out.println("Name chosen is :" + name);
 }

 public void setAge(int age) {
  puppyAge = age;
 }

 public int getAge() {
  return puppyAge;
 }

 public static void main(String[] args) {
  /* Object creation */
  Puppy myPuppy = new Puppy("tommy");

  /* Call class method to set puppy's age */
  myPuppy.setAge(2);

  /* Call another class method to get puppy's age */
  myPuppy.getAge();

  /* You can access instance variable as follows as well */
  System.out.println("Puppy's name is :" + myPuppy.puppyAge);
 }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.