Professional Documents
Culture Documents
Last week, we said an object is a collection of data and operations that manipulate that data. Object-Oriented Programming COMS W1007 Introduction to Computer Science Christopher Conway 5 June 2003 In Java, an objects data members are called elds and the operations that manipulate an objects elds are called methods. A class declaration is basically just a list of eld and method denitions.
Packages
A package is a collection of classes. To include a class in a package, we use a package declaration at the beginning of the le. package foo.bar ; public class MyClass { ... } Now, unless we are inside package foo.bar, we must refer to MyClass as foo.bar.MyClass. Warning: The JVM expects to nd foo.bar.MyClass in a directory foo/bar under the CLASSPATH.
Access Modiers
Access to an objects elds and methods is governed by access modiers.
But Why?
The ability to hide data allows us to make our code more reusable. If we know that no outside class can change our private data and all data access will be through a dened set of public methods, we can exhaustively test our class and prove that it works correctly. Then we can combine with any other class and be sure it will continue to work correctly. If outside classes cant touch our private data, we dont have to worry about outside code breaking our class.
public - a public eld or method can be seen and accessed by any class. private - a private eld or method can only be seen and accessed inside its own class. protected - a protected eld or method can only be seen and accessed by its own class and related classes.
A eld or method with no access modier can be seen and accessed by any class in the same package.
Class Modiers
The only valid access modier for a class is public. A public class can be seen and used by any other class. Without the public modier, a class can only be used within its own package. There can only be one public class in a le, and the les name must be the same as that class. E.g., public class Foo must be in the le Foo.java.
Instance Data
Most class members are associated with an instance of the class.
public class Foo { public int x ; public static void main(String[] args) { Foo a = new Foo() ; Foo b = new Foo() ; a.x = 2 ; b.x = 15 ; /* a.x=2, b.x=15 */ } }
Static Data
Members declared static are associated with the class as a whole. There is only one copy shared between all instances.
public class Foo { public static int x ; public static void main(String[] args) { Foo a = new Foo(), b = new Foo() ; a.x = 2 ; b.x = 15 ; /* a.x=15, b.x=15 */ } }
Final Fields
Fields can be declared final, just like regular variables. A final eld can only be assigned a value once. We usuall dene class constants by declaring them both static and final. public class Color { public static final public static final public static final public static final ... }
Methods
The only way were going to get an object to do anything interesting is to dene operations on the data. A method is a piece of code that tells us about or changes the objects state.
public class Foo { private int x ;
Method Declarations
A method declaration denes a methods parameters, its return type and its body (the code). int max(int a, int b) { return a > b ? a : b ; }
Parameters
A methods parameters (or arguments) are values provided by the caller. Parameters look exactly like variables inside the method. int max(int a, int b) { return a > b ? a : b ; }
Return Type
The return type denes what kind of value we can expect when we call the method. The return value is determined by the return statement. When a return statement is encountered, execution of the method stops and the value of its expression is returned to the caller.
int max(int a, int b) { return a > b ? a : b ; } public static void main(String[] args) { int n = max(3,5) ; /* n=5 */ }
Call-by-Value
Parameters are passed by value: a method operates on a copy of the parameter. Changes to a parameter value arent visible outside the method. void foo(int a) { a = 3 ; } void bar() { int a = 0 ; foo(a) ; /* a=0 */ }
Its usually best to have one return at the end of the method.
int foo(int a) { int result = 0 ; if( a > 0 ) result = 1 ; return result ; }
Call-by-Value: References
Recall that variables of object types are references. This effects how they behave as parameters.
void f(Foo a) { a = new Foo() ; a.setX(12) ; } void g(Foo a) { a.setX(5) ; } public static void main(String[] args) { Foo a = new Foo(), b = a ; a.setX(2) ; /* a.x=2, b.x=2 */ f(a) ; /* a==b, a.x=2 */ g(a) ; /* a.x=5, b.x=5 */ }
Local Variables
Variables declared inside a method are local to that method (i.e., they have method scope). void f() { int i ; ... } void g() { int i ; /* OK: previous i out of scope */ ... }
Method Overloading
We can dene two methods with the same name as long as they have different parameter types. Common sense tells us we should only do this if the methods do equivalent things to different types. public static int max(int a, int b) { return a > b ? a : b ; } public static double max(double x, double y) { return x > y ? x : y ; }
Static Methods
A static method is associated with the class as a whole. Static methods cannot use or change any instance data, because they are not part of an instance.
public class Foo { private int x ; private static int y ; public static void f(int a) { x = a ; /* Error: cant change x */ } public static void g(int a) { y = a ; /* OK: y is static */ } }
Static Methods: 2
We often use static methods for general utility methods.
public class Util { public static int max(int a, int b) { return a > b ? a : b ; } public static int pow(int a, int b) { int result=1 ; for( int i=0 ; i < b ; i++ ) result *= a ; } }
Constructors
A constructor is a method that gets called when an object is created using new. We can use the constructor to initialize the elds of the object. A constructor can have as many parameters as necessary, but it cannot have a return type.
public class Foo { private int x ; public Foo(int x) { this.x = x ; } public static void main(String[] args) { Foo a = new Foo(3) ; /* a.x=3 */ } }
Default Values
The default constructor assigns the following values to elds, depending on their type. boolean char byte,short,int,long float double reference false 0 0 +0.0F +0.0 null
Constructor Overloading
We can be overloaded constructors, like other methods.
public class Foo { private int x, y ; public Foo(int x) { this.x = x ; y = 5 ; } public Foo(int x, int y) { this.x = x ; this.y = y ; } public static void main(String[] args) { Foo a = new Foo(2) ; /* a.x=2, a.y=5 */ Foo b = new Foo(3,4) ; /* a.x=3, a.y=4 */ } }
Constructor Overloading: 2
We can call one constructor from another using the this keyword. We often use this to supply default arguments to a constructor. A call to another constructor with this can only be the rst statement in the constructor.
public Foo(int x) { this(x,5) ; } public Foo(int x, int y) { this.x = x ; this.y = y ; } public static void main(String[] args) { Foo a = new Foo(2) ; /* a.x=2, a.y=5 */ }