Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Classes to Store Data Computer Science 2 Gerb.

Similar presentations


Presentation on theme: "Using Classes to Store Data Computer Science 2 Gerb."— Presentation transcript:

1 Using Classes to Store Data Computer Science 2 Gerb

2 Some Vocabulary Data stored in an instance of a class is called an instance variable –Defined inside the class but not in any methods –Does not use the static keyword A method that can be called on an instance of a class is called an instance method –Does not use the keyword static Instance variables and methods can be public or private –Put public or private before the declaration –Public means it can be accessed by users of the class –Private means it can only be accessed by the class’s own methods.

3 An Example class Student{ private String name; private int id; public String getName() { return name;} public void setName(String s) { name=s;} public int getId() { return id;} public void setId(int n){ id = n;} } Two private instance variables –id –name Four public instance methods –setName –getName –setId –getId

4 Creating an instance of Student Student s1 = new Student(); s1.setName(“Joy Chu”); s1.setId( ); System.out.println(s1.getName()); System.out.println(s1.getId()); Prints: Joy Chu

5 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId());

6 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID

7 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID S2 Name ID

8 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID Devon Pierce S2 Name ID

9 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID Devon Pierce 111 S2 Name ID

10 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID Devon Pierce 111 S2 Name ID Sarah Hirsch

11 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); S1 Name ID Devon Pierce 111 S2 Name ID Sarah Hirsch 222

12 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); Prints: Devon Pierce S1 Name ID Devon Pierce 111 S2 Name ID Sarah Hirsch 222

13 An Example with two students Student s1 = new Student(); Student s2 = new Student(); s1.setName(“Devon Pierce”); s1.setId(111); s2.setName(“Sarah Hirsch”); s2.setId(222); System.out.println(s1.getName()); System.out.println(s2.getId()); Prints: Devon Pierce 222 S1 Name ID Devon Pierce 111 S2 Name ID Sarah Hirsch 222

14 Summary Classes can be used to store dissimilar items. –Data in a class are called instance variables –Methods that access that data are called instance methods Instance methods and variables can be public or private –private means can only be used by methods within that class –public means can be used outside the class


Download ppt "Using Classes to Store Data Computer Science 2 Gerb."

Similar presentations


Ads by Google