Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Java – Part II

Similar presentations


Presentation on theme: "An Introduction to Java – Part II"— Presentation transcript:

1 An Introduction to Java – Part II
Ben Lerner An Introduction to Java – Part II

2 Class Definitions Classes consist of constructors, methods, and fields
<modifier> class Person { constructors fields methods }

3 Constructors and Objects
Constructors initialize objects, same name as the class public Person (string name, int age) { … } Objects created using new keyword and the constructor, store reference Person P = new Person(“Michael”, 21);

4 Fields Fields are the variables that define the attributes of a class
string name_; int age_; Constructors initialize an object’s fields public Person (string name, int age) { name_ = name; age_ = age; }

5 Methods Mutator methods change fields void setAge(int a) { age_ = a; }
Inspector methods access fields string getName() { return name;} Constructors are also methods

6 Parameters Both primitive variables and objects can be passed to methods Parameters are always passed by value Remember that objects in Java are just references to the objects


Download ppt "An Introduction to Java – Part II"

Similar presentations


Ads by Google