Chapter 12
Coding
Classes
12.1 Fields
- Initial values may be assigned to instance and class fields.
- Java provides default initial values to uninitialized fields.
- Fields carrying the ‘final’ modifier must be explicitly initialized.
class prg {
public static void main ( String[] args ){
MyClass obj;
System.out.println( MyClass.i1 );
System.out.println( MyClass.i2 );
obj = new MyClass();
System.out.println( obj.i3 );
System.out.println( obj.i4 );
} }
class MyClass {
static int i1, i2 = 2;
int i3, i4 = 4;
}
12.2 Constructors
- If no constructor is defined, an implicit parameter-less empty-body constructor is assumed .
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass();
} }
class MyClass {
}
- All the parameters of a constructor have values assigned to them when the constructor is invoked.
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass( "first" );
obj = new MyClass( "second" );
} }
class MyClass {
MyClass ( String x ){
System.out.println( x );
}
}
12.3 Methods
- All the parameters of a method have values assigned to them when the method is invoked.
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass();
obj.myMethod ( "first" );
obj.myMethod ( "second" );
} }
class MyClass {
void myMethod ( String x ){
System.out.println( x );
}
}
- A method with a non-void return type should contain a ‘return’ instruction in its body. The return instruction
should provide a value whose type is consistent with the return type of the method.
class prg {
public static void main ( String[] args ){
double v = 10.5;
System.out.println( v );
v = myMath.some ( "Hello there!" );
System.out.println( v );
} }
class myMath {
public static double same(String s){
return 4.7;
}
}
- The ‘return’ instruction may be introduced also to void methods. In such cases, these instructions should be
provided with no values.
class prg {
public static void main ( String[] args ){
MyClass.method();
} }
class MyClass {
static void method(){
System.out.println( "valueless return" );
return;
}
}
12.4 Internal References to Static Features
- Unlike parameters and local variables, static features are accessible through their class name.
class prg {
public static void main ( String[] args ){
MyClass.i = 11;
MyClass.myMethod ( 10 );
MyClass.myMethod ();
} }
class MyClass {
static int i;
static void myMethod ( int x ){
MyClass.i = x;
}
static void myMethod (){
int y;
y = MyClass.i;
myMethod( y );
}
}
- Within their classes, when directly accessible, the static features can be accessed without referencing the class
names.
class prg {
public static void main ( String[] args ){
MyClass.i = 11;
MyClass.myMethod ( 10 );
MyClass.myMethod ();
} }
class MyClass {
static int i;
static void myMethod ( int x ){
i = x;
}
static void myMethod (){
int y;
y = i;
myMethod( y );
}
}
- Parameters and local variables may have identical names as fields and methods of the class. In such cases, the static
fields and methods can not be directly referenced. That is, they require the mentioning of the class
name.
class prg {
public static void main ( String[] args ){
MyClass.i = 11;
MyClass.myMethod ( 10 );
MyClass.myMethod ();
} }
class MyClass {
static int i;
static void myMethod ( int i ){
MyClass.i = i;
}
static void myMethod (){
int i;
i = MyClass.i;
myMethod( i );
}
}
12.5 Internal References to Instance Features
- Instance fields and methods are accessed through objects of the class.
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass();
obj.myMethod ( 10 );
} }
class MyClass {
int i;
void myMethod ( int x ){ }
}
- Each constructor and instance method has an implicitly introduced distinguished variable named ‘this’. The
variable is of the given class type, and it holds an object of the class.
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass();
obj.myMethod ( 10 );
} }
class MyClass {
int i;
MyClass(){
int a;
a = this.i;
}
void myMethod ( int b ){
this.i = b;
}
void myMethod (){
this.myMethod( this.i );
} }
- The ‘this’ variable can be omitted from references to instance fields and methods, when the access to these features
is not blocked by parameters and local variables of identical names.
class prg {
public static void main ( String[] args ){
MyClass obj;
obj = new MyClass();
obj.myMethod ( 10 );
} }
class MyClass {
int i;
MyClass(){
int i;
i = this.i;
}
void myMethod ( int i ){
this.i = i;
}
void myMethod (){
myMethod( i );
} }
Exercises
12.6 Assignment #13: Coding Classes
Due: Fr, May 9, midnight
Add to class ‘MyClass’ the needed features so that the given program will compile (under javac) into an executable
code.
class lab13{
public static void main(String[] args){
MyClass var;
int i;
var = new MyClass(5.1);
MyClass.a = 5;
var.b = 2;
MyClass.c();
var.d();
i = var.e();
var = MyClass.f(5);
} }
class MyClass{
}
| summary information |
| variables (fields, parameters, local variables) | <modifiers> <type> <name>; |
- The modifiers are optional
- The type can’t be void
- Class fields must carry the static modifier
|
| methods | <modifiers> <return type> <name> ( <parameters> ) { <body> } |
|
|
| constructors | <modifiers> <name> ( <parameters> ) { <body> } |
- A constructor must have a name identical to that of the owner class.
- The modifiers are optional
|
Assume ‘lab13’ for the submit program. The program should be stored in a file named ‘lab13.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A