Download presentation
Presentation is loading. Please wait.
Published byReynard George Modified over 8 years ago
1
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods
2
Primitive types in Java
3
Class (simple example) Suppose you need to represent “a person” You need a person to consist of: A first name A last name An Age Interesting questions: 1) Is this the official representation of a person? 2) Does the compiler already know this representation?
4
User Defined Type (non-primitive) public class Person { String firstName; String lastName; int age; } note: a public class that only contains public data structures and no methods (a.k.a. functions) is a essentially a struct in the programming language C/C++
5
Allocating a Class (non-primitive) object int i = 3; // allocates storage for an int and assigns it to 3 // i can be used to refer to the storage Person p; // sets p to be a reference to a Person… // no storage is allocated Person p = new Person(); // sets p to be a reference to a Person… // storage is allocated
6
Changing a data member of an object Person p = new Person(); p.firstName = “George”; p.lastName = “Washington”; p.age = 40; Note: allowing data to be accessed so freely, can lead to users corrupting the data. Ex) integers may be assigned where String belong, etc.
7
Protecting your Class data public class Person { private String firstName; private String lastName; private int age; } Note: Person p = new Person(); p.age = 40; // will cause an error because age is private
8
Public Method setting Private data public class Person { private String firstName; private String lastName; private int age; public void setAge(int a) { age = a; } } Note: Person p = new Person(); p.setAge(40); // will set the age to 40
9
Constructor Method public class Person { private String firstName; private String lastName; private int age; public Person(String f, String l, int a) { firstName = f; lastName = l; age = a; } public setAge(int a) { age = a; } } Person p = new Person(“George”, “Washington”, 40); //Will create a Person object and initialize all 3 fields
10
Static (Class) Variables public class Person { private String firstName; private String lastName; private int age; public static int personCount; public Person(String f, String l, int a) { firstName = f; lastName = l; age = a; personCount++; } public setAge(int a) { age = a; } }
11
Static (Class) Variables Person p1 = new Person(“George”, “Washington”, 40); Person p2 = new Person(“Abraham”, “Lincoln”, 41); Person p3 = new Person(“Theodore”, “Roosevelt”, 42); System.out.print(p2.personCount); // would output 3 System.out.print(Person.personCount); // would output 3
12
Variables related to Classes in Java Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.
13
Private Statics / Public Access Methods public class Person { private String firstName; private String lastName; private int age; private static int personCount; public Person(String f, String l, int a) { firstName = f; lastName = l; age = a; personCount++; } public static void printCount() { System.out.print(personCount); } public setAge(int a) { age = a; } }
14
Classes composed of Classes Finally Note: Class type definitions are made up of combinations of primitive types and other already existing classes. public class Group { Person person1; Person person2; Person person3; static int groupsCreated; }
15
Hello World in Java (revisited) public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!!!"); } Notes: HelloWorld is a class that you created Main is a public static method of the Class HelloWorld is the entry point of your class. (leftover from C/C++) System is a class out is a variable of the class System out is static because it is referenced by it’s class name not an object println is a void method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.