notes4

notes4 - Local Variable Initialization • Unlike instance...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar + 1; //ok now } Eg. void mymethod() { int foo; try { foo = getres(); } catch (Exception e) { } int bar = foo; //compile error since foo //may never have been initialized Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Eg. int i; someobj obj = new someobj(); mymethod(i, obj); void mymethod (int j, someobj o) { }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
There can be multiple methods defined in a class with the same name. The compiler picks the correct one based on the number and type of arguments passed to the method (also called parametric polymorphism ). There is no programmer specified operator overloading in Java. (Except the + operator is overloaded for string concatenation). Constructors Used to initialize class instances (i.e. create objects). Constructors can be overloaded as shown in the following example. E.g. class car { String model; int doors; car (String m, int d) { //constructor 1 model = m; doors = d; } car (String m) { //constructor 2 this(m, 4); //calls the first
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

notes4 - Local Variable Initialization • Unlike instance...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online