Skip to content

Instantly share code, notes, and snippets.

@okaram
Last active August 29, 2015 14:06
Show Gist options
  • Save okaram/e100389209b546d276ff to your computer and use it in GitHub Desktop.
Save okaram/e100389209b546d276ff to your computer and use it in GitHub Desktop.
public class Puppy {
// instance variables; private
private String _name;
private int _age;
// constructor
public Puppy (String name, int age)
{
// constructor is called on a specific instance; let's modify that instance's variables
_name=name;
_age=age;
}
// setters and getters; used to prepare against future changes
public void setName(String name) { _name=name;}
public String getName() {return _name;}
public void setAge(int age) {_age=age;}
public int getAge() {return _age;}
public static void main(String args[]) {
Puppy p1=new Puppy("fido",3); // two different puppies, each with its own instance variables
Puppy p2=new Puppy("joe",5);
System.out.println(p1.getName()+"'s age is "+p1.getAge());
System.out.println(p2.getName()+"'s age is "+p2.getAge());
}
}
public class Puppy {
// instance variables; public, which is a bad habit
public String _name;
public int _age;
// constructor
public Puppy (String name, int age)
{
// constructor is called on a specific instance; let's modify that instance's variables
_name=name;
_age=age;
}
public static void main(String args[]) {
Puppy p1=new Puppy("fido",3); // two different puppies, each with its own instance variables
Puppy p2=new Puppy("joe",5);
System.out.println(p1._name +"'s age is "+p1._age);
System.out.println(p2._name+"'s age is "+p2._age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment