Guest User

Untitled

a guest
Jan 22nd, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. public class Person { // this is the class declaration
  2.  
  3. // these are instance variables. any method in this class can access and change them
  4. public String name; // public means that anything can access this variable
  5. private int age; // private means that only methods in this class can access this variable
  6.  
  7. // this is a default constructor. if no parameters are provided, this constructor is called
  8. public Person() {
  9. name = "Jimmy";
  10. age = 10;
  11. }
  12.  
  13. // if this object is created with the following parameters, this constructor is called
  14. public Person(String newName, int newAge) {
  15. name = newName;
  16. age = newAge;
  17. }
  18.  
  19. public void PrintHello() { // this is a method of the class
  20. System.out.println("Hello, " + name + "!");
  21. }
  22.  
  23. public int HighFive() { // this is another method. it returns the number 5
  24. return 5;
  25. }
  26.  
  27. public int GetAge() {
  28. return age; // even though age is private, we can share its value by using a method
  29. }
  30.  
  31. // let's add two numbers and return the sum
  32. public int AddNumbers(int a, int b) {
  33. int c = a + b;
  34. return c;
  35. }
  36. }
Add Comment
Please, Sign In to add comment