Results 1 to 2 of 2
  1. #1
    Werdeyo is offline Member
    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0

    Default Beginner class method/variable question

    Hello everyone I have another quick question, I was wondering why I needed to
    make the method getAge and then make the variable spikeAge from getAge to
    print out the age variable that I made in the very beginning of creating the class
    so why couldn't I just do System.out.println(age); in the main method.
    Thanks for all the help in advance.

    Java Code:
    class Dog {
      int age;
    
      public Dog(int dogsAge) {
        age = dogsAge;
      }
      
      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(4);
        spike.bark();
        spike.run(12000);
        
        int spikeAge = spike.getAge();
        System.out.println(spikeAge);
        
    	}
    }
    Last edited by Werdeyo; 01-08-2017 at 12:35 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Beginner class method/variable question

    First, please eliminate excess blank lines and wrap you code in code tags to improve formatting. See BB Code List - Java Programming Forum - Learn Java Programming

    I have done it for you here.

    Java Code:
    class Dog {
       int age;
    
       public Dog(int dogsAge) {
          age = dogsAge;
       }
    
       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(4);
          spike.bark();
          spike.run(12000);
    
          int spikeAge = spike.getAge();
          System.out.println(spikeAge);
       }
    }
    You could do as you said, simply dereference age with the spike reference. But using getters and setters is better because it hides implementation details. Say you later decided to store age in a String and convert to and int on access. The user would never know because all that is hidden. But one you publish the class you can't change it without breaking existing code.

    Also, if you want to make your class immutable you must use getters with private fields. Otherwise, someone could simply set the value directly thus breaking immutability.

    I also tend to use the getters and setters in the class that implements them. That way, if I ever change the way the variable is stored, I only have to modify one location in the class instead of everywhere I reference them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 06:09 PM
  2. Replies: 5
    Last Post: 10-16-2012, 01:10 PM
  3. Replies: 5
    Last Post: 07-12-2012, 08:44 PM
  4. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  5. Replies: 20
    Last Post: 06-03-2009, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •