Lecture 3: Classes and Objects presents returning a value from a method, The this keyword, Access control, Class members, instance members, The static keyword, Defining constants. | Lecture 3: Classes and Objects Lê Hồng Phương phuonglh@ Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi Content ● Returning a value from a method ● The this keyword ● Access control ● Class members, instance members – ● The static keyword Defining constants 2012-2013 Object-Oriented Programming: Classes and Objects 2 Returning a value from a method ● A method can return a primitive type or a reference type. A method of the Rectangle class public int getArea() { return width * height; } public Bicycle getTheFastest(Bicycle myBike, Bicycle yourBike, Environment env) { Bicycle fastest; // code to calculate which bike is faster, // given each bike's gear and cadence and given the // environment (terrain and wind) return fastest; A method of a class } modelling a bicycle tournament 2012-2013 Object-Oriented Programming: Classes and Objects 3 Using the this keyword ● The this keyword is a reference to the current object – the object whose method or constructor is being called. public class Point { int x = 0; int y = 0; public Point(int x, int y) { = x; = y; } } 2012-2013 Object-Oriented Programming: Classes and Objects 4 Using the this keyword ● Within a constructor, use the this keyword to call another constructor in the same class. public class Rectangle { private int x, y; private int width, height; } public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { = x; = y; = width; = height; } // . 2012-2013 Object-Oriented Programming: Classes and .